#include "CenterClubManager.h"
#include "StringConverter.h"
#include "ResourceManager.h"
#include "Database/DatabaseEnv.h"
#include "StringConverter.h"
#include "json/json.h"
#include "NGLog.h"
#include "Character.h"
#include "CharManager.h"
#include "Items.h"
#include "ItemManager.h"
#include "Users.h"
#include "UserManager.h"
#include "ChannelManager.h"
#include "ItemModelManager.h"
#include "Message.h"
#include "CenterBankManager.h"
#include "VipCardManager.h"
#include "Tools.h"
#include "XMLData.h"
#include "GameDefine.h"
#include XMLDATA_MGR_PATH
#include LOGIC_MGR_PATH
#include LOG_MGR_PATH
#include PROTOCOLS_MGR_PATH
#include "OpcodesEx.h"
#include "WorldPacket.h"
#include "CenterBattleManager.h"
#include "CenterBattleLogManager.h"
#include "AIInterface.h"
#include "GameThreadLogic.h"
#include "MD5.h"

#define SQLBUFER_LEN 16384
using namespace AIScript;

//--------------------------------------------------------
bool CenterClubManager::AddCenterClub(CenterClub & cc)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &fields);
	
	// uint32 club_id;							// 俱乐部ID
	fields["show_id"] = cc.show_id;				// 俱乐部显示ID
	fields["status"] = cc.status;				// 状态
	fields["agent_id"] = cc.agent_id;			// 代理商ID
	fields["gz_id"] = cc.gz_id;					// 分区ID
	fields["name"] = cc.name;					// 俱乐部名称
	fields["notice"] = cc.notice;				// 公告信息
	fields["desc"] = cc.desc;					// 公告信息
	fields["rank"] = cc.rank;					// 星级
	fields["city"] = cc.city;					// 市
	fields["town"] = cc.town;					// 城
	
	fields["badge_url"] = cc.badge_url;			// 徽章url
	fields["badge_icon"] = cc.badge_icon;		// 徽章ID
	fields["create_time"] = cc.create_time;		// 创建时间
	
	fields["owner_pid"] = cc.owner_pid;			// 会长PID
	fields["owner_nick"] = cc.owner_nick;		// 会长昵称
	
	fields["vice_president"] = cc.vice_president;		// 副会长人数
	fields["curr_member"] = cc.curr_member;				// 当前成员数量
	fields["max_member"] = cc.max_member;				// 最大成员数量
	fields["expiry_date"] = cc.expiry_date;				// 有效期（截止日期）
	fields["activity_value"] = cc.activity_value;		// 活跃值
	fields["config_data"] = cc.config_data;				// 配置数据
	
	uint32 show_id = 0;		// 随机显示ID
	while(show_id == 0)
	{
		uint32 temp_id = RandomUInt(100000, 999999);
		std::list<CenterClub> lstClub;
		uint32 count = GetCenterClubList(&lstClub, "`status` < 255 and `show_id` = %u", temp_id);
		if(count > 0)
			continue;
		
		show_id = temp_id;
		break;
	}
	
	fields["show_id"] = show_id;		// 俱乐部显示ID
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &fields))
		return false;
	
	cc.club_id = fields["club_id"].GetUInt64();
	return true;
}

//--------------------------------------------------------
bool CenterClubManager::UpdateCenterClub(const CenterClub & cc)
{
	Fields fields;
	// fields["m_agent_id"] = cc.m_agent_id;			// 代理商ID
	// fields["gz_id"] = cc.gz_id;					// 分区ID
	fields["status"] = cc.status;					// 状态
	fields["name"] = cc.name;						// 俱乐部名称
	fields["notice"] = cc.notice;					// 公告信息
	fields["desc"] = cc.desc;						// 公告信息
	fields["rank"] = cc.rank;						// 星级
	fields["city"] = cc.city;						// 市
	fields["town"] = cc.town;						// 城
	fields["badge_url"] = cc.badge_url;				// 徽章url
	fields["badge_icon"] = cc.badge_icon;			// 徽章ID
	
	// fields["create_time"] = cc.create_time;		// 创建时间
	// fields["owner_pid"] = cc.owner_pid;			// 会长PID
	// fields["owner_nick"] = cc.owner_nick;		// 会长昵称
	
	fields["vice_president"] = cc.vice_president;	// 副会长人数
	fields["curr_member"] = cc.curr_member;			// 当前成员数量
	fields["max_member"] = cc.max_member;			// 最大成员数量
	fields["expiry_date"] = cc.expiry_date;			// 有效期（截止日期）
	fields["activity_value"] = cc.activity_value;	// 活跃值
	fields["config_data"] = cc.config_data;			// 配置数据
	
	Log.Debug("CenterClubManager::UpdateCenterClub","expiry_date[%s]", cc.expiry_date.c_str());
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &fields, "`club_id` = %u", cc.club_id);
	Log.Debug("CenterClubManager::UpdateCenterClub","count[%u]", count);
	return count > 0;
}

//--------------------------------------------------------
bool CenterClubManager::UpdateCenterClub(Fields * fields, const char * QueryString, ...)
{
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, QueryString); 
	vsnprintf(sql, SQLBUFER_LEN, QueryString, vlist); 
	va_end(vlist);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, fields, sql);
	return count > 0;	
}

//--------------------------------------------------------
bool CenterClubManager::UpdateCenterClub(const uint64 & cc_id, Fields * fields)
{
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, fields, "`club_id` = %u", cc_id);
	return count > 0;	
}

//--------------------------------------------------------
bool CenterClubManager::GetCenterClub(const uint64 & cc_id, CenterClub * cc)
{
	assert (cc);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &rows, "status < 255 and `club_id` = %u", cc_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	cc->club_id = fields["club_id"].GetUInt64();
	cc->show_id = fields["show_id"].GetUInt32();
	cc->status = fields["status"].GetUInt32();
	cc->agent_id = fields["agent_id"].GetUInt32();
	cc->gz_id = fields["gz_id"].GetUInt32();
	cc->name = fields["name"].GetValue();
	cc->notice = fields["notice"].GetValue();
	cc->desc = fields["desc"].GetValue();
	cc->rank = fields["rank"].GetUInt32();
	cc->city = fields["city"].GetValue();
	cc->town = fields["town"].GetValue();
	cc->badge_url = fields["badge_url"].GetValue();
	cc->badge_icon = fields["badge_icon"].GetUInt32();
	cc->create_time = fields["create_time"].GetValue();
	
	cc->owner_pid = fields["owner_pid"].GetUInt32();
	cc->owner_nick = fields["owner_nick"].GetValue();
	cc->vice_president = fields["vice_president"].GetUInt32();
	cc->curr_member = fields["curr_member"].GetUInt32();
	cc->max_member = fields["max_member"].GetUInt32();
	cc->expiry_date = fields["expiry_date"].GetValue();
	cc->activity_value = fields["activity_value"].GetUInt64();
	cc->config_data = fields["config_data"].GetValue();
	return true;
}

bool CenterClubManager::GetCenterClubByShowID(const uint32 & show_id, CenterClub * cc)
{
	assert (cc);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &rows, "status < 255 and `show_id` = %u", show_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	cc->club_id = fields["club_id"].GetUInt64();
	cc->show_id = fields["show_id"].GetUInt32();
	cc->status = fields["status"].GetUInt32();
	cc->agent_id = fields["agent_id"].GetUInt32();
	cc->gz_id = fields["gz_id"].GetUInt32();
	cc->name = fields["name"].GetValue();
	cc->notice = fields["notice"].GetValue();
	cc->desc = fields["desc"].GetValue();
	cc->rank = fields["rank"].GetUInt32();
	cc->city = fields["city"].GetValue();
	cc->town = fields["town"].GetValue();
	cc->badge_url = fields["badge_url"].GetValue();
	cc->badge_icon = fields["badge_icon"].GetUInt32();
	cc->create_time = fields["create_time"].GetValue();
	
	cc->owner_pid = fields["owner_pid"].GetUInt32();
	cc->owner_nick = fields["owner_nick"].GetValue();
	cc->vice_president = fields["vice_president"].GetUInt32();
	cc->curr_member = fields["curr_member"].GetUInt32();
	cc->max_member = fields["max_member"].GetUInt32();
	cc->expiry_date = fields["expiry_date"].GetValue();
	cc->activity_value = fields["activity_value"].GetUInt64();
	cc->config_data = fields["config_data"].GetValue();
	return true;
}

//--------------------------------------------------------
uint32 CenterClubManager::GetCenterClubList(std::list<CenterClub> * lstClub, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstClub->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClub cc;
		Fields &fields = *iter;
		
		cc.club_id = fields["club_id"].GetUInt64();
		cc.show_id = fields["show_id"].GetUInt32();
		cc.status = fields["status"].GetUInt32();
		cc.agent_id = fields["agent_id"].GetUInt32();
		cc.gz_id = fields["gz_id"].GetUInt32();
		cc.name = fields["name"].GetValue();
		cc.notice = fields["notice"].GetValue();
		cc.desc = fields["desc"].GetValue();
		cc.rank = fields["rank"].GetUInt32();
		cc.city = fields["city"].GetValue();
		cc.town = fields["town"].GetValue();
		cc.badge_url = fields["badge_url"].GetValue();
		cc.badge_icon = fields["badge_icon"].GetUInt32();
		cc.create_time = fields["create_time"].GetValue();
		
		cc.owner_pid = fields["owner_pid"].GetUInt32();
		cc.owner_nick = fields["owner_nick"].GetValue();
		cc.vice_president = fields["vice_president"].GetUInt32();
		cc.curr_member = fields["curr_member"].GetUInt32();
		cc.max_member = fields["max_member"].GetUInt32();
		cc.expiry_date = fields["expiry_date"].GetValue();
		cc.activity_value = fields["activity_value"].GetUInt64();
		cc.config_data = fields["config_data"].GetValue();
		
		lstClub->push_back(cc);
	}
	
	return lstClub->size();
}

//===========================俱乐部逻辑相关==================================
// 获取俱乐部信息（发包）
void CenterClubManager::GetClubHallInfo(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull())
		return ;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return ;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return ;
	
	std::list<CenterClubMessage> lstClubMessage;
	sCenterClubMessageMgr.GetClubNearestMessages(club_id, &lstClubMessage);
	
	std::list<CenterClubMember> lstClubMember;
	sCenterClubMemberMgr.GetTargetClubMemberList(&lstClubMember, club_id);
	
	// 发送俱乐部信息
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubHallInfoPacket(&packet, cc, lstClubMember, lstClubMessage, pUser->getUInt32Field("platform_id")))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

// 获取俱乐部资料
void CenterClubManager::GetClubNormalInfo(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull())
		return ;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return ;
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubNormalInfoPacket(&packet, cc))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

void CenterClubManager::GetClubNormalInfo(CharPtr & pChr, String & strName)
{
	if(pChr.isNull() || strName.size() == 0)
		return ;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return ;
	
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	
	std::list<CenterClub> lCC;
	GetCenterClubList(&lCC, "status < 255 and agent_id = %u and name = '%s' ", agent_id, strName.c_str());
	
	if(StringConverter::isNumber(strName))
	{
		CenterClub cc;
		if(GetCenterClubByShowID(StringConverter::parseUnsignedInt(strName), &cc))
		{
			lCC.push_back(cc);
		}
		else if(GetCenterClub(StringConverter::parseUnsignedInt(strName), &cc))
		{
			lCC.push_back(cc);
		}
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubListInfoPacket(&packet, enClubList_Name, lCC))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	Log.Debug("CenterClubManager::GetClubNormalInfo","char[%u] strName[%s] lCC size[%u]", pChr->getHandle(), strName.c_str(), lCC.size());
}

// 获得俱乐部列表-满天飞（通过名字/ID查询）
void CenterClubManager::GetClubNormalInfoMTF(CharPtr & pChr, String & strName)
{
	if(pChr.isNull() || strName.size() == 0)
		return ;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return ;
	
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	
	std::list<CenterClub> lCC;
	GetCenterClubList(&lCC, "status < 255 and agent_id = %u and name = '%s' ", agent_id, strName.c_str());
	
	if(StringConverter::isNumber(strName))
	{
		CenterClub cc;
		if(GetCenterClubByShowID(StringConverter::parseUnsignedInt(strName), &cc))
		{
			lCC.push_back(cc);
		}
		else if(GetCenterClub(StringConverter::parseUnsignedInt(strName), &cc))
		{
			lCC.push_back(cc);
		}
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubListInfoMTFPacket(&packet, enClubList_Name, lCC))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	Log.Debug("CenterClubManager::GetClubNormalInfoMTF","char[%u] strName[%s] lCC size[%u] strName size[%u]", pChr->getHandle(), strName.c_str(), lCC.size(), strName.size());
}

// 获得满天飞俱乐部信息（发包）
void CenterClubManager::GetMTFClubNormalInfo(CharPtr & pChr, const uint64 &  club_id)
{
	if(pChr.isNull())
		return ;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return ;
	
	std::list<CenterClubMember> lstManger;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstManger, "status < 255 and club_id = %u and type = %u", club_id, enClubMemberType_VicePresident);
	
	std::list<CenterClubMember> lstMember;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstMember, "status < 255 and club_id = %u", club_id);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateMTFClubInfoPacket(&packet, cc, lstManger, lstMember))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

// 获取俱乐部列表信息
void CenterClubManager::GetClubListInfo(CharPtr & pChr, String & strArea)
{
	if(pChr.isNull() || strArea.size() == 0)
		return ;
	
	std::list<CenterClub> lCC;
	GetCenterClubList(&lCC, "status < 255 and city = '%s' ", strArea.c_str());
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubListInfoPacket(&packet, enClubList_Area, lCC))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	Log.Debug("CenterClubManager::GetClubListInfo","char[%u] strArea[%s] lCC size[%u]", pChr->getHandle(), strArea.c_str(), lCC.size());
}

// 获得自己所在俱乐部列表
void CenterClubManager::GetSelfClubList(CharPtr & pChr)
{
	if(pChr.isNull())
		return ;
	
	Log.Debug("CenterClubManager::GetSelfClubList","char[%u] now[%u]", pChr->getHandle(), now());
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return ;
		
	std::list<CenterClub> lstClub;
	std::list<CenterClubMember> lstMember;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstMember, "status < 255 and agent_id = %u and pid = %u", pUser->getUInt32Field("reg_from"), pUser->getUInt32Field("platform_id"));
	
	std::list<CenterClubMember>::iterator iter, ei = lstMember.end();
	for(iter = lstMember.begin(); iter!=ei; ++iter)
	{
		CenterClub cc;
		if(GetCenterClub((*iter).club_id, &cc))
			lstClub.push_back(cc);
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubListInfoPacket(&packet, enClubList_Myself, lstClub))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	Log.Debug("CenterClubManager::GetSelfClubList","char[%u] now[%u] lstClub size[%u]", pChr->getHandle(), now(), lstClub.size());
}

// 获得自己所在俱乐部列表-满天飞
void CenterClubManager::GetSelfClubListMTF(CharPtr & pChr)
{
	if(pChr.isNull())
		return ;
	
	Log.Debug("CenterClubManager::GetSelfClubListMTF","char[%u] now[%u]", pChr->getHandle(), now());
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return ;
		
	std::list<CenterClub> lstClub;
	std::list<CenterClubMember> lstMember;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstMember, "status < 255 and agent_id = %u and pid = %u", pUser->getUInt32Field("reg_from"), pUser->getUInt32Field("platform_id"));

	std::list<CenterClubMember>::iterator iter, ei = lstMember.end();
	for(iter = lstMember.begin(); iter!=ei; ++iter)
	{
		CenterClub cc;
		if(GetCenterClub((*iter).club_id, &cc))
			lstClub.push_back(cc);
	}

	WorldPacket packet;
	if(sProtocolsMgr.CreateClubListInfoMTFPacket(&packet, enClubList_Myself, lstClub))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	Log.Debug("CenterClubManager::GetSelfClubListMTF","char[%u] now[%u] lstClub size[%u]", pChr->getHandle(), now(), lstClub.size());
	
}

// 获取玩家创建的俱乐部数量
uint32 CenterClubManager::PlayerCreateClubCount(CharPtr & pChr)
{
	if(pChr.isNull())
		return 0;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return 0;
	
	FieldRows fieldRows;
	sDataMgr.queryDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUB, &fieldRows, 
									"count(`club_id`) as `count`", "`owner_pid` = %u and `status` < 255 and agent_id = %u", 
													pUser->getUInt32Field("platform_id"), pUser->getUInt32Field("reg_from"));
	
	// 获取联系人总数
	uint32 total_num = 0;
	if(fieldRows.size())
	{
		std::map<String,Field>::iterator itMap, eiMap = fieldRows[0].end();
		for(itMap = fieldRows[0].begin(); itMap!=eiMap; ++itMap)
		{
			total_num += (itMap->second).GetUInt32();
		}
	}
	
	return total_num;
}

// 创建俱乐部
bool CenterClubManager::CreateClub(CharPtr & pChr, const uint32 & badge_icon, String & name, String & city)
{
	if(pChr.isNull())
		return 0;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return 0;
	
WGS_CLUBROOM_LOCK
	CenterClubConfig ccConfig;
	CenterClubConfig ccClubConfig;
	
	uint32 rank = pChr->getUInt32Field("rank");
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	uint64 club_id = 0;
	uint8 result = enCreateClubResult_Sucess;
	do
	{
		if(name.size() == 0)
		{// 名字不能为空
			result = enCreateClubResult_InvalidName;
			break;
		}
		std::list<CenterClub> lstClub;
		if(GetCenterClubList(&lstClub, "status < 255 and name = '%s' and agent_id = %u", name.c_str(), agent_id))
		{// 名字已经存在
			result = enCreateClubResult_InvalidName;
			break;
		}
		
		if(city.size() == 0)
		{// 无效的地区
			result = enCreateClubResult_InvalidArea;
			break;
		}
		
		ccConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Player, rank, agent_id);
		if(ccConfig.config_id == 0)
		{// 未找到配置
			result = enCreateClubResult_ErrorRank;
			break;
		}
		
		if(ccConfig.max_club_add <= PlayerCreateClubCount(pChr))
		{// 创建数量达到上限
			result = enCreateClubResult_Limit;
			break;
		}
		
		ccClubConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Club, 1, agent_id);
		if(ccClubConfig.pay_diamand)
		{
			if(ccClubConfig.pay_diamand > sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen))
			{// 创建俱乐部费用判断	
				result = enCreateClubResult_NeedDiamand;
				break;
			}
			
			if(sGLMgr.reduceItemNum(pChr, ItemModel_DiamondOpen, ccClubConfig.pay_diamand))
			{// 费用扣除失败
				result = enCreateClubResult_NeedDiamand;
				break;
			}
		}
		
		if(agent_id == 5045)
		{
			uint32 last_createTime = pChr->getUInt32Field("country_time");
			if(!sGLMgr.IsSameDate(last_createTime, time(0)))
			{
				pChr->setUInt32Field("country_noble", 0);
			}
			
			if(pChr->getUInt32Field("country_noble") >= sXmlDataMgr.GetConfXMLValue("MJCLUB_MAXDAILY_CREATENUM"))
			{// 创建次数限制
				result = enCreateClubResult_DailyCreateLimit;
				break;
			}
		}
		
	}while(0);
	
	if(result == enCreateClubResult_Sucess)
	{
		CenterClub cc;
		cc.status = 1;										// 状态
		cc.agent_id = agent_id;								// 代理商ID
		cc.gz_id = pUser->getUInt32Field("gz_id");			// 分区ID
		cc.name = name;										// 公会名
		cc.rank = 1;										// 星级
		cc.city = city;										// 地区
		cc.badge_icon = badge_icon;							// 图标
		
		cc.owner_pid = pid;
		cc.owner_nick = pChr->getStringField("name");
		cc.activity_value = sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen);
		
		CenterClubConfig ccClubConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Club, cc.rank, agent_id);
		cc.max_member = ccClubConfig.max_member_add;
		
		cc.create_time = sGLMgr.GetCurDateTime();			// 创建时间
		AddCenterClub(cc);									// 添加数据
		
		club_id = cc.club_id;
		
		Log.Debug("CenterClubManager::CreateClub","club_id[%u] cc.club_id[%u]", club_id, cc.club_id);
		// 添加成员-会长
		sCenterClubMemberMgr.ClubAddMember(cc, pid, 0, enClubMemberType_President);
		
		pChr->setUInt32Field("country_time", uint32(time(0)));
		pChr->setUInt32Field("country_noble", pChr->getUInt32Field("country_noble") + 1);
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubCreateResultPacket(&packet, club_id, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enCreateClubResult_Sucess)
	{
		GetClubHallInfo(pChr, club_id);
	}
	
	return true;
}

// 创建满天飞俱乐部
bool CenterClubManager::CreateClubMTF(CharPtr & pChr, String & name, String & dsec)
{
	if(pChr.isNull())
		return 0;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return 0;
	
WGS_CLUBROOM_LOCK
	CenterClubConfig ccConfig;
	CenterClubConfig ccClubConfig;
	
	uint32 rank = pChr->getUInt32Field("rank");
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	uint64 club_id = 0;
	uint8 result = enCreateClubResult_Sucess;
	do
	{
		if(name.size() == 0)
		{// 名字不能为空
			result = enCreateClubResult_InvalidName;
			break;
		}
		std::list<CenterClub> lstClub;
		if(GetCenterClubList(&lstClub, "status < 255 and name = '%s'", name.c_str()))
		{// 名字已经存在
			result = enCreateClubResult_InvalidName;
			break;
		}
		
		ccConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Player, rank, agent_id);
		if(ccConfig.config_id == 0)
		{// 未找到配置
			result = enCreateClubResult_ErrorRank;
			break;
		}
		
		if(ccConfig.max_club_add <= PlayerCreateClubCount(pChr))
		{// 创建数量达到上限
			result = enCreateClubResult_Limit;
			break;
		}
		
		ccClubConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Club, 1, agent_id);
		if(ccClubConfig.pay_diamand)
		{
			if(ccClubConfig.pay_diamand > sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen))
			{// 创建俱乐部费用判断	
				result = enCreateClubResult_NeedDiamand;
				break;
			}
			
			if(!sGLMgr.reduceItemNum(pChr, ItemModel_DiamondOpen, ccClubConfig.pay_diamand))
			{// 费用扣除失败
				result = enCreateClubResult_NeedDiamand;
				break;
			}
		}
		
	}while(0);
	
	if(result == enCreateClubResult_Sucess)
	{
		CenterClub cc;
		cc.status = 1;										// 状态
		cc.agent_id = agent_id;								// 代理商ID
		cc.gz_id = pUser->getUInt32Field("gz_id");			// 分区ID
		cc.name = name;										// 公会名
		cc.rank = 1;										// 星级
		cc.desc = dsec;										// 简介
		
		cc.owner_pid = pid;
		cc.owner_nick = pChr->getStringField("name");
		cc.activity_value = sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen);
		
		CenterClubConfig ccClubConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Club, cc.rank, agent_id);
		cc.max_member = ccClubConfig.max_member_add;
		
		cc.create_time = sGLMgr.GetCurDateTime();			// 创建时间
		AddCenterClub(cc);									// 添加数据
		
		club_id = cc.club_id;
		
		Log.Debug("CenterClubManager::CreateClub","club_id[%u] cc.club_id[%u]", club_id, cc.club_id);
		// 添加成员-会长
		sCenterClubMemberMgr.ClubAddMember(cc, pid, 0, enClubMemberType_President);
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubCreateMTFResultPacket(&packet, club_id, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enCreateClubResult_Sucess)
	{
		GetMTFClubNormalInfo(pChr, club_id);
	}
	
	return true;
}

// 俱乐部加入申请
bool CenterClubManager::RequireJoinClub(CharPtr & pChr, const uint64 & club_id, const uint32 & referee_id, String & content)
{
	if(pChr.isNull() || club_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return 0;

WGS_CLUBROOM_LOCK
	CenterClub cc;
	String referee_icon = "";
	
	uint32 now_time = now();
	std::map<uint32, uint32>::iterator fit = m_mapReviewJoinTiemr.find(pChr->getHandle());
	if(fit == m_mapReviewJoinTiemr.end())
	{
		m_mapReviewJoinTiemr.insert(std::make_pair(pChr->getHandle(), now_time));
	}
	else
	{
		if(fit->second + 1000 > now_time)
			return false;
		
		fit->second = now_time;
	}
	
	std::list<ResourcePtr> lstUser;
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enRequireJoinClubResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enRequireJoinClubResult_ErrorClubId;
			break;
		}
		
		if(cc.curr_member >= cc.max_member)
		{// 满员
			result = enRequireJoinClubResult_ClubFull;
			break;
		}
		
		if(sCenterClubMemberMgr.IsPlayerInClub(club_id, pid))
		{// 玩家已经在公会中了
			result = enRequireJoinClubResult_InClub;
			break;
		}
		
		std::list<CenterClubMessage> lstClubMessage;
		if(sCenterClubMessageMgr.GetCenterClubMessageList(&lstClubMessage, "club_id = %u and status < 255 and type = %u and owner_pid = %u", 
																	club_id, enReviewMessageType_Join, pid))
		{// 已经提交过申请
			result = enRequireJoinClubResult_AreadySend;
			break;
		}
		
		if(referee_id)
		{// 有推荐人
			
			sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u and reg_from = %u", referee_id, pUser->getUInt32Field("gz_id"), pUser->getUInt32Field("reg_from"));
			if(lstUser.empty())
			{// 无效的推荐人
				result = enRequireJoinClubResult_ErrorRefereeId;
				break;
			}
			
			CharPtr pRefereeChr = sUserMgr.load((*lstUser.begin())->getUInt32Field("char_id"));
			if(!pRefereeChr.isNull())
				referee_icon = pRefereeChr->getStringField("name");
		}
		
	}while(0);
	
	if(result == enRequireJoinClubResult_Sucess)
	{// 添加审核消息
		sCenterClubMessageMgr.AddReviewMessage(club_id, pUser, content, referee_id, referee_icon);
		
		// 给管理员发更新红点的消息
		WorldPacket packet;
		if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, club_id, enClubMessageType_Review, 1, cc.name))
			sCenterClubMemberMgr.BroadcastClubManagerMemberPacket(club_id, &packet);
		
		std::list<CenterClubMember> lstClubMember;
		sCenterClubMemberMgr.GetCenterClubMemberList(&lstClubMember, "status < 255 and club_id = %u and type > %u", club_id, enClubMemberType_Normal);
		
		char buf[256] = "";	//公会【%s】有新的审核消息
		sprintf(buf, sMsg.getMessage("club_review_message_join").c_str(), cc.name.c_str());
		
		String strData = buf;
		
		std::list<CenterClubMember>::iterator iter, ei = lstClubMember.end();
		for(iter = lstClubMember.begin(); iter!=ei; ++iter)
		{
			sGLMgr.SendSystemMessageToTargetServer((*iter).pid, strData, sMsg.getType("club_review_message_join"), (*iter).agent_id);
		}
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateRequireJoinClubResultPacket(&packet, club_id, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 玩家退出俱乐部
bool CenterClubManager::PlayerExitClub(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;

	CenterClub cc;
	CenterClubMember ccMember;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enPlayerExitClubResult_Sucess;
	do
	{
	WGS_CLUBROOM_LOCK
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enPlayerExitClubResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		{// 不是公会成员
			result = enPlayerExitClubResult_NotClubMember;
			break;
		}
		
	}while(0);
	
	if(result == enPlayerExitClubResult_Sucess)
	{// 成功，处理玩家数据，方法里面会发包
		if(ccMember.type == enClubMemberType_President)
		{// 会长退-解散公会
			WorldPacket packet;
			if(sProtocolsMgr.CreateClubPlayerExitResult(&packet, club_id, result))
				sGLMgr.SendProtocolsToChr(pChr, &packet);
			
			result = DissolveClub(pChr, club_id);
		}
		else
		{// 其他人退-少人（至少会长还在，所以不会解散）
		WGS_CLUBROOM_LOCK
			result = sCenterClubMemberMgr.ClubReduceMember(cc, pid) ? enPlayerExitClubResult_Sucess : enPlayerExitClubResult_NotClubMember;
			// 添加公会成员加入消息
			char buf[256] = "";
			if(ccMember.agent_id == 5053)
				sprintf(buf, sMsg.getMessage("club_exit_playerjlb").c_str(), ccMember.nick.c_str(), sGLMgr.GetCurDateTime().c_str());
			else
				sprintf(buf, sMsg.getMessage("club_exit_player").c_str(), ccMember.nick.c_str(), sGLMgr.GetCurDateTime().c_str());
			
			String strMessage = buf;
			sCenterClubMessageMgr.AddSystemChatMessage(strMessage, ccMember.agent_id, ccMember.club_id);
		}
	}
	else
	{// 失败时 发送结果
		WorldPacket packet;
		if(sProtocolsMgr.CreateClubPlayerExitResult(&packet, club_id, result))
			sGLMgr.SendProtocolsToChr(pChr, &packet);
	}
	
	return true;
}

// 审核加入消息
bool CenterClubManager::DealMemberJoinClub(CharPtr & pChr, const uint64 & club_messageId, const uint8 & mode)
{
	if(pChr.isNull() || club_messageId == 0 || mode == 0)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;

WGS_CLUBROOM_LOCK
	CenterClubMessage ccMessage;
	CenterClubMember ccMember;
	CenterClubMember ccDestMember;
	CenterClub cc;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enReviewJoinClubResult_Sucess;
	do
	{
		if(!sCenterClubMessageMgr.GetCenterClubMessage(club_messageId, &ccMessage))
		{// 消息不存在
			result = enReviewJoinClubResult_NotExist;
			break;
		}
		
		if(ccMessage.type != enClubMessageType_Review)
		{// 消息类型不对
			result = enReviewJoinClubResult_NotExist;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(ccMessage.club_id, pid, &ccMember))
		{// 无权操作-审核方不在公会中
			result = enReviewJoinClubResult_WuQuan;
			break;
		}
		
		uint32 now_time = now();
		std::map<uint32, uint32>::iterator fit = m_mapReviewJoinTiemr.find(ccMessage.owner_cid);
		if(fit == m_mapReviewJoinTiemr.end())
		{
			m_mapReviewJoinTiemr.insert(std::make_pair(ccMessage.owner_cid, now_time));
		}
		else
		{
			if(fit->second + 1000 > now_time)
				return false;
			
			fit->second = now_time;
		}
		
		if(ccMember.type != enClubMemberType_President && ccMember.type != enClubMemberType_VicePresident)
		{// 玩家权限不足
			result = enReviewJoinClubResult_WuQuan;
			break;
		}
		
		if(sCenterClubMemberMgr.GetCenterClubMember(ccMessage.club_id, ccMessage.owner_cid, &ccDestMember))
		{// 被审核的玩家已经在公会中
			result = enReviewJoinClubResult_Exist;
			break;
		}
		
		if(mode == enOperatorType_Agree)
		{// 同意，判断人数
			if(!GetCenterClub(ccMessage.club_id, &cc))
			{// 消息所属公会不存在
				result = enReviewJoinClubResult_NoClub;
				break;
			}
			
			if(cc.curr_member >= cc.max_member)
			{// 公会满员
				result = enReviewJoinClubResult_ClubFull;
				break;
			}
		}
		
	}while(0);
	
	if(result == enReviewJoinClubResult_Sucess)
	{
		uint8 review_tep = enClubMessageType_ReviewRefuse;
		if(mode == enOperatorType_Agree)
		{// 同意
			// 添加成员
			sCenterClubMemberMgr.ClubAddMember(cc, ccMessage.owner_pid, ccMessage.target_pid, enClubMemberType_Normal);
			if(ccMessage.target_pid)
			{// 给成员加钱 功能暂时屏蔽
				std::list<ResourcePtr> lstUser;
				sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", ccMessage.owner_pid, pUser->getUInt32Field("gz_id"));
				if(lstUser.size())
				{
					UserPtr pMemberUser = sUserMgr.load((*lstUser.begin())->getUInt32Field("uid"));
					if(!pMemberUser.isNull())
					{
						CharPtr pMemberChr = sCharMgr.load(pMemberUser->getUInt32Field("char_id"));
						if(!pMemberChr.isNull())
						{
							AICreatureBaseInterface * pChrBaseAI = TO_CREATURE_BASE_INTERFACE(pMemberChr->getAIInterface());
							if(pChrBaseAI)
							{
								uint32 isGetRefeeBounty = pChrBaseAI->GetXMLData("REFEE_BOUNT");
								if(!isGetRefeeBounty)
								{
									pChrBaseAI->SetXMLData("REFEE_BOUNT", 1);
									pChrBaseAI->SetXMLData("REFEE_PID", ccMessage.target_pid);
									// 加钱
									sGLMgr.addItemNum(pMemberChr, ItemModel_Coins, 1000);
									// 记录日志
									sLogDataMgr.UpdateItemsInfoLog(pMemberChr, en_ST_ClubRefereeBounty, ItemModel_Coins, en_UNT_Add, 1000, cc.club_id, ccMessage.target_pid);
									
									std::map<uint32, uint32> mapItems;
									const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_FIRST_REFEEBOUNTY");
									if(pModel)
									{
										String szTitle = pModel->szTitle;
										String szContent = pModel->szContent;
										// 恭喜您在加入俱乐部【%s】首次填写推荐人，获得奖励%u游戏币
										
										char buf[256] = "";
										sprintf(buf, szContent.c_str(),cc.name.c_str(), 1000);
										sGLMgr.CreateMail(ccMessage.owner_pid, szTitle, buf, 0, 0, &mapItems);				
									}
								}
							}
							
						}
					}
				}
			}
			
			review_tep = enClubMessageType_ReviewAgree;
			
			// 添加公会成员加入消息
			char buf[256] = "";
			if(cc.agent_id == 5053)
				sprintf(buf, sMsg.getMessage("club_join_playerjlb").c_str(), ccMessage.owner_nick.c_str(), sGLMgr.GetCurDateTime().c_str());
			else
				sprintf(buf, sMsg.getMessage("club_join_player").c_str(), ccMessage.owner_nick.c_str(), sGLMgr.GetCurDateTime().c_str());
			
			String strMessage = buf;
			sCenterClubMessageMgr.AddSystemChatMessage(strMessage, cc.agent_id, cc.club_id);
		}
		
		WorldPacket packet;
		if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, cc.club_id, review_tep, 1, cc.name))
			sGLMgr.SendProtocolsToChrByPidGzid(ccMessage.owner_pid, pUser->getUInt32Field("gz_id"), &packet);
		
		// 增加审核历史
		sCenterClubLogMgr.AddReviewLog(ccMessage, mode);
		
		ccMessage.status = 255;
		sCenterClubMessageMgr.UpdateCenterClubMessage(ccMessage);
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubReviewResultPacket(&packet, club_messageId, enReviewMessageType_Join, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 带入分数
bool CenterClubManager::PlayerTakeinScore(CharPtr & pChr, const uint64 & club_id, const uint32 & score)
{
	if(pChr.isNull() || club_id == 0 || score < 100)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
WGS_CLUBROOM_LOCK
	CenterClub cc;
	
	uint32 real_score = score * 1.1f;
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint32 now_time = now();
	std::map<uint32, uint32>::iterator fit = m_mapReviewTakeinTiemr.find(pid);
	if(fit == m_mapReviewTakeinTiemr.end())
	{
		m_mapReviewTakeinTiemr.insert(std::make_pair(pid, now_time));
	}
	else
	{
		if(fit->second + 1000 > now_time)
			return false;
		
		fit->second = now_time;
	}
	
	uint8 result = enClubTakeinResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enClubTakeinResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.IsPlayerInClub(club_id, pid))
		{// 不是公会成员
			result = enClubTakeinResult_NotMember;
			break;
		}
		
		if(sGLMgr.GetItemNum(pChr, ItemModel_Coins) < real_score)
		{// 游戏币不足
			result = enClubTakeinResult_NeedCoins;
			break;
		}
		
	}while(0);
	
	if(result == enClubTakeinResult_Sucess)
	{
		// 扣钱
		sGLMgr.reduceItemNum(pChr, ItemModel_Coins, real_score);
		// 扣钱日志		
		sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubTakeinCoins, ItemModel_Coins, en_UNT_Reduce, real_score, cc.club_id);
		
		// 添加带入消息，等待管理员审核
		sCenterClubMessageMgr.AddTakeinCoinsMessage(club_id, pUser, score);
		
		// 给管理员发更新红点的消息
		WorldPacket packet;
		if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, club_id, enClubMessageType_Takein, 1, cc.name))
			sCenterClubMemberMgr.BroadcastClubManagerMemberPacket(club_id, &packet);
		
		std::list<CenterClubMember> lstClubMember;
		sCenterClubMemberMgr.GetCenterClubMemberList(&lstClubMember, "status < 255 and club_id = %u and type > %u", club_id, enClubMemberType_Normal);
		
		char buf[256] = "";	//公会【%s】有新的带入消息
		sprintf(buf, sMsg.getMessage("club_review_message_takein").c_str(), cc.name.c_str());
		String strData = buf;
		
		std::list<CenterClubMember>::iterator iter, ei = lstClubMember.end();
		for(iter = lstClubMember.begin(); iter!=ei; ++iter)
		{
			sGLMgr.SendSystemMessageToTargetServer((*iter).pid, strData, sMsg.getType("club_review_message_takein"), (*iter).agent_id);
		}
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubTakeinResultPacket(&packet, club_id, score, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 审核玩家带入分数
bool CenterClubManager::DealTakeinScore(CharPtr & pChr, const uint64 & club_messageId, const uint8 & mode)
{
	if(pChr.isNull() || club_messageId == 0 || mode == 0)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
WGS_CLUBROOM_LOCK
	CenterClubMessage ccMessage;
	CenterClubMember ccMember;
	CenterClub cc;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enReviewTakeinResult_Sucess;
	do
	{
		if(!sCenterClubMessageMgr.GetCenterClubMessage(club_messageId, &ccMessage))
		{// 消息不存在
			result = enReviewTakeinResult_ErrorMessage;
			break;
		}
		
		if(!GetCenterClub(ccMessage.club_id, &cc))
		{// 公会不存在
			result = enReviewTakeinResult_NoClub;
			break;
		}
		
		uint32 now_time = now();
		std::map<uint32, uint32>::iterator fit = m_mapReviewTakeinTiemr.find(ccMessage.owner_pid);
		if(fit == m_mapReviewTakeinTiemr.end())
		{
			m_mapReviewTakeinTiemr.insert(std::make_pair(ccMessage.owner_pid, now_time));
		}
		else
		{
			if(fit->second + 1000 > now_time)
				return false;
			
			fit->second = now_time;
		}
		
		if(!sCenterClubMemberMgr.IsPlayerClubManager(ccMessage.club_id, pid))
		{// 操作的人权限不够
			result = enReviewTakeinResult_WuQuan;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(ccMessage.club_id, ccMessage.owner_pid, &ccMember))
		{// 不是公会成员
			result = enReviewTakeinResult_WuQuan;
			break;
		}
		
		if(ccMessage.type != enClubMessageType_Takein)
		{// 消息类型不对
			result = enReviewTakeinResult_ErrorMessage;
			break;
		}
		
	}while(0);
	
	if(result == enReviewTakeinResult_Sucess)
	{
		std::list<ResourcePtr> lstUser;
		sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", ccMessage.owner_pid, pUser->getUInt32Field("gz_id"));
		
		CharPtr pDestChr;
		ResourcePtr pDestUser = *lstUser.begin();
		if(!pDestUser.isNull())
			pDestChr = sCharMgr.load(pDestUser->getUInt32Field("char_id"));
		
		if(mode == enOperatorType_Agree)
		{// 同意，增加玩家带入
			// 抽水一个比例
			uint32 pump_percent = sXmlDataMgr.GetConfXMLValue("CLUB_TAKEIN_PUMP_PERCENT");
			if(pump_percent)
			{
				uint32 pump_score = ccMessage.takein_coins * ((pump_percent) * 0.01f);
				
				// 记录俱乐部手续费
				sCenterClubLogMgr.AddTakeinPumpLog(ccMember, pump_score);
				
				// 记录带入抽水日志
				if(!pDestChr.isNull())
					sLogDataMgr.UpdateItemsInfoLog(pDestChr, en_ST_ClubTakeinPump, ItemModel_Coins, en_UNT_Reduce, pump_score, ccMessage.club_id);
			}
			
			ccMember.curr_score += ccMessage.takein_coins;			// 带入消息里面的钱加到玩家成员数据上
			ccMember.takein_coins += ccMessage.takein_coins;		// 带入消息里面的钱加到玩家成员数据上
			sCenterClubMemberMgr.UpdateCenterClubMember(ccMember);
			
			if(!pDestChr.isNull())
			{
				WorldPacket packet;
				if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, ccMessage.club_id, enClubMessageType_TakeinAgree, 1, cc.name))
					sGLMgr.SendProtocolsToChr(pDestChr, &packet);
				if(sProtocolsMgr.CreateClubPlayerCurrScorePacket(&packet, ccMessage.club_id, ccMember.curr_score))
					sGLMgr.SendProtocolsToChr(pDestChr, &packet);
			}
		}
		else
		{// 拒绝，把钱还给玩家
			
			if(!pDestChr.isNull())
			{
				WorldPacket packet;
				if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, ccMessage.club_id, enClubMessageType_TakeinRefuse, 1, cc.name))
					sGLMgr.SendProtocolsToChr(pDestChr, &packet);
				
				uint32 real_takeinCoins = ccMessage.takein_coins * 1.1f;
				// 退钱
				sGLMgr.addItemNum(pDestChr, ItemModel_Coins, real_takeinCoins);
				// 记录日志
				sLogDataMgr.UpdateItemsInfoLog(pDestChr, en_ST_ClubTakeinCoins, ItemModel_Coins, en_UNT_Add, real_takeinCoins, cc.club_id);
				
				std::map<uint32, uint32> mapItems;
				const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_MANAGER_TAKEINREFUSE");
				if(pModel)
				{
					String szTitle = pModel->szTitle;
					String szContent = pModel->szContent;
					// 俱乐部管理员【%s】 ID:%u 拒绝了您的带入请求，您获得%u金币。
					
					char buf[256] = "";
					sprintf(buf, szContent.c_str(), pChr->getStringField("name").c_str(), pid, ccMessage.takein_coins);
					sGLMgr.CreateMail(pid, szTitle, buf, 0, 0, &mapItems);				
				}
			}
		}
		
		// 添加审核带入历史
		sCenterClubLogMgr.AddTakeinLog(ccMessage, mode);
		
		ccMessage.status = 255;
		sCenterClubMessageMgr.UpdateCenterClubMessage(ccMessage);
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubReviewResultPacket(&packet, club_messageId, enReviewMessageType_Takein, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 退分
bool CenterClubManager::PlayerReturnScore(CharPtr & pChr, const uint64 & club_id, const uint32 & target_pid2)
{
	if(pChr.isNull() || club_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
WGS_CLUBROOM_LOCK
	CenterClubMember ccMember;
	CenterClub cc;
	
	uint32 curr_score = 0;
	uint32 pid = pUser->getUInt32Field("platform_id");
	
	uint32 target_pid = target_pid2;
	if(target_pid == 0)
		target_pid = pid;
	
	uint32 now_time = now();
	std::map<uint32, uint32>::iterator fit = m_mapReviewJoinTiemr.find(target_pid);
	if(fit == m_mapReviewJoinTiemr.end())
	{
		m_mapReviewJoinTiemr.insert(std::make_pair(target_pid, now_time));
	}
	else
	{
		if(fit->second + 1000 > now_time)
			return false;
		
		fit->second = now_time;
	}
	
	uint8 result = enReturnCoinsResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在，不是该公会成员
			result = enReturnCoinsResult_NotMember;
			break;
		}
		
		if(target_pid && target_pid == pid)
		{// 自己退分
			Log.Debug("CenterClubManager::PlayerReturnScore","target_pid[%u] 111", target_pid);
			if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
			{// 不是成员
				result = enReturnCoinsResult_NotMember;
				break;
			}
		}
		else
		{// 退其他玩家分
			Log.Debug("CenterClubManager::PlayerReturnScore","target_pid[%u] 222", target_pid);
			if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, target_pid, &ccMember))
			{// 不是成员
				result = enReturnCoinsResult_NotMember;
				break;
			}
			
			if(!sCenterClubMemberMgr.IsPlayerClubManager(club_id, pid))
			{// 权限不够
				result = enReturnCoinsResult_NoQuanXian;
				break;
			}
		}
		
		Log.Debug("CenterClubManager::PlayerReturnScore","member_id[%u] curr_score[%u] 222", ccMember.member_id, ccMember.curr_score);
		curr_score = ccMember.curr_score;
		if(ccMember.curr_score == 0)
		{// 没有积分
			result = enReturnCoinsResult_NoCoins;
			break;
		}
		// enReturnCoinsResult_Playing = 2,			// 游戏中
		
	}while(0);
	
	if(result == enReturnCoinsResult_Sucess)
	{
		GameBank bank;
		sBankMgr.GetRealBank(target_pid, bank);
		if(bank.gz_id != 0 && !sXmlDataMgr.IsInGameIds(sGLMgr.GetTargetServerGameid(bank.gz_id)))
		{// 玩家在游戏中，通知游戏
			
			Json::Value valData;
			valData["agent_id"] = pUser->getUInt32Field("reg_from");
			valData["gz_id"] = bank.gz_id;
			valData["pid"] = target_pid;
			
			Json::FastWriter w;
			uint8 req_result = sGLMgr.ClubManagerTuifenPlayingPlayer(w.write(valData));
			switch(req_result)
			{
			case 0:		// 失败
				result = enReturnCoinsResult_Failed;
				break;
			case 2:		// 等待游戏结束
				result = enReturnCoinsResult_WaitGameFinish;
				break;
			default:
				break;
			}
		}
		
		if(result == enReturnCoinsResult_Sucess)
		{// 结果成功，直接扣
			
			// 在房间游戏赢的金币需要扣5%佣金返还给介绍人
			if(target_pid == 0 || target_pid == pid)
			{// 自己退分
				uint32 winScore = ccMember.curr_score > ccMember.takein_coins ? ccMember.curr_score - ccMember.takein_coins : 0;
				if(winScore > 20)
				{
					AICreatureBaseInterface * pChrBaseAI = TO_CREATURE_BASE_INTERFACE(pChr->getAIInterface());
					if(pChrBaseAI)
					{
						if(ccMember.curr_result > 0 && pChrBaseAI->GetXMLData("REFEE_PID"))
						{// 给介绍人分红，这里还需要判断抽水部分够不够
							
							uint32 refer_pid = pChrBaseAI->GetXMLData("REFEE_PID");
							uint32 gz_id = ccMember.gz_id;
							
							CharPtr pReferChar = sGLMgr.GetCharptrByPidAndGzid(refer_pid, gz_id);
							if(!pReferChar.isNull())
							{
								uint32 pump_coins = uint32(winScore * 0.05f);
								sGLMgr.addItemNum(pReferChar, ItemModel_Coins, pump_coins);
								sLogDataMgr.UpdateItemsInfoLog(pReferChar, en_ST_ClubFenHong, ItemModel_Coins, en_UNT_Add, pump_coins, ccMember.club_id, ccMember.pid);
								
								ccMember.curr_score = ccMember.curr_score > pump_coins ? ccMember.curr_score - pump_coins : 0;
								
								std::map<uint32, uint32> mapItems;
								const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_GAME_FENHONG");
								if(pModel)
								{
									String szTitle = pModel->szTitle;
									String szContent = pModel->szContent;
									// 玩家【%s】为你赢得了%u游戏币的分红金。
									
									char buf[256] = "";
									sprintf(buf, szContent.c_str(),ccMember.nick.c_str(), pump_coins);
									sGLMgr.CreateMail(ccMember.referee_pid, szTitle, buf, 0, 0, &mapItems);				
								}
							}
						}
					}
				}
				
				sGLMgr.addItemNum(pChr, ItemModel_Coins, ccMember.curr_score);	// 加钱
				sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubReturnCoins, ItemModel_Coins, en_UNT_Add, ccMember.curr_score, cc.club_id);	// 记录日志
			}
			else
			{// 被人退分
				std::list<ResourcePtr> lstUser;
				sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", target_pid, pUser->getUInt32Field("gz_id"));
				if(lstUser.size())
				{
					ResourcePtr pTargetUser = *lstUser.begin();
					if(!pTargetUser.isNull())
					{
						uint32 winScore = ccMember.curr_score > ccMember.takein_coins ? ccMember.curr_score - ccMember.takein_coins : 0;
						CharPtr pTargetChr = sCharMgr.load(pTargetUser->getUInt32Field("char_id"));
						if(!pTargetUser.isNull())
						{
							if(winScore > 20)
							{
								AICreatureBaseInterface * pChrBaseAI = TO_CREATURE_BASE_INTERFACE(pTargetChr->getAIInterface());
								if(pChrBaseAI)
								{
									if(ccMember.curr_result > 0 && pChrBaseAI->GetXMLData("REFEE_PID"))
									{// 给介绍人分红
										
										uint32 refer_pid = pChrBaseAI->GetXMLData("REFEE_PID");
										uint32 gz_id = ccMember.gz_id;
										
										CharPtr pReferChar = sGLMgr.GetCharptrByPidAndGzid(refer_pid, gz_id);
										if(!pReferChar.isNull())
										{
											uint32 pump_coins = uint32(winScore * 0.05f);
											sGLMgr.addItemNum(pReferChar, ItemModel_Coins, pump_coins);
											sLogDataMgr.UpdateItemsInfoLog(pReferChar, en_ST_ClubFenHong, ItemModel_Coins, en_UNT_Add, pump_coins, ccMember.club_id, ccMember.pid);
											
											ccMember.curr_score = ccMember.curr_score > pump_coins ? ccMember.curr_score - pump_coins : 0;
											
											std::map<uint32, uint32> mapItems;
											const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_GAME_FENHONG");
											if(pModel)
											{
												String szTitle = pModel->szTitle;
												String szContent = pModel->szContent;
												// 玩家【%s】为你赢得了%u游戏币的分红金。
												
												char buf[256] = "";
												sprintf(buf, szContent.c_str(),ccMember.nick.c_str(), pump_coins);
												sGLMgr.CreateMail(ccMember.referee_pid, szTitle, buf, 0, 0, &mapItems);				
											}
										}
									}
								}
							}
							
							sGLMgr.addItemNum(pTargetChr, ItemModel_Coins, ccMember.curr_score);	// 加钱
							sLogDataMgr.UpdateItemsInfoLog(pTargetChr, en_ST_ClubReturnCoins, ItemModel_Coins, en_UNT_Add, ccMember.curr_score, cc.club_id);	// 记录日志
							
							// 更新玩家当前分数
							WorldPacket packet;
							if(sProtocolsMgr.CreateClubPlayerCurrScorePacket(&packet, club_id, 0))
								sGLMgr.SendProtocolsToChr(pTargetChr, &packet);
						}
					}
				}
				
			}
			
			// 添加退分历史
			sCenterClubLogMgr.AddExitCoinsLog(ccMember);
			
			// 退分加邮件
			if(target_pid && (target_pid != pid))
			{
				std::map<uint32, uint32> mapItems;
				const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_MANAGER_TUIFEN");
				if(pModel)
				{
					String szTitle = pModel->szTitle;
					String szContent = pModel->szContent;
					// 俱乐部管理员【%s】 ID:%u 对您进行了退分操作，您获得%u金币。
					
					char buf[256] = "";
					sprintf(buf, szContent.c_str(), pChr->getStringField("name").c_str(), pid, ccMember.curr_score);
					
					sGLMgr.CreateMail(target_pid, szTitle, buf, 0, 0, &mapItems);
				}
			}
			
			// 更新成员带入数量
			ccMember.curr_score = 0;
			ccMember.curr_result = 0;
			ccMember.takein_coins = 0;
			ccMember.play_loop = 0;
			
			uint32 real_pump = ccMember.total_pump;
			if(real_pump > 1 && sXmlDataMgr.GetConfXMLValue("CLUB_TOTAL_PUMP_PERCENT"))
			{// 实际对局费抽水
				uint32 real_pump_pump = real_pump * sXmlDataMgr.GetConfXMLValue("CLUB_TOTAL_PUMP_PERCENT") * 0.01f;
				real_pump = real_pump > real_pump_pump ? real_pump - real_pump_pump : 0;
			}
			
			// 扣除抽水，产生收益
			ccMember.total_pump = 0;
			// 清空房间输赢成绩
			ccMember.pump_info = "";
			
			sCenterClubMemberMgr.UpdateCenterClubMember(ccMember);
			
			if(real_pump)
			{// 产生收益
				// 结算玩家对局费收益
				stIncomeInfo stICInfo;
				stICInfo.gz_id = ccMember.gz_id;
				stICInfo.game_id = sGLMgr.GetTargetServerGameid(ccMember.gz_id, pUser->getUInt32Field("reg_from"));
				stICInfo.cb_id = ccMember.club_id;
				stICInfo.channel_id = ccMember.club_id;
				stICInfo.end_time = sGLMgr.GetCurDateTime();
				stICInfo.agent_id = ccMember.agent_id;
				
				stIncomePlayerInfo playInfo;
				playInfo.pid = ccMember.pid;
				playInfo.model_id = ItemModel_Coins;
				playInfo.num = real_pump;
				
				stICInfo.vPlayers.push_back(playInfo);
				// 调用收益
				sGLMgr.AddPlayerIncomeInfoToUrl(stICInfo);
				
				// 增加俱乐部活跃值
				sCenterClubMgr.AddClubActivityValue(ccMember.club_id, real_pump);
			}
			
		}
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubReturnCoinsPacket(&packet, club_id, curr_score, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 购买会员
bool CenterClubManager::PlayerBuyRank(CharPtr & pChr, const uint8 & rank)
{
	if(pChr.isNull() || rank == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClubConfig ccConfig;
	
WGS_CLUBROOM_LOCK
	// uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enBuyPlayerRankResulr_Sucess;
	do
	{
		ccConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Player, rank, pUser->getUInt32Field("reg_from"));
		if(ccConfig.config_id == 0)
		{// 未找到配置
			result = enBuyPlayerRankResulr_NoConfig;
			break;
		}
		
		if(sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen) < ccConfig.pay_diamand)
		{// 费用不足
			result = enBuyPlayerRankResulr_NeedDiamand;
			break;
		}
		
	}while(0);
	
	WorldPacket packet;
	if(result == enBuyPlayerRankResulr_Sucess)
	{
		if(sGLMgr.reduceItemNum(pChr, ItemModel_DiamondOpen, ccConfig.pay_diamand))
		{
			if(rank > pChr->getUInt32Field("rank"))
			{// 等级提升
				// 更新公会最大人数以及副会长人数
				pChr->setUInt32Field("rank", rank);
				pChr->SaveDB();
			}
			
			if(ccConfig.give_coins)
			{// 赠送铜钱
				// 添加铜钱
				sGLMgr.addItemNum(pChr, ItemModel_Coins, ccConfig.give_coins);
				// 记录日志
				sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubBuyGoods, ItemModel_Coins, en_UNT_Add, ccConfig.give_coins, ccConfig.config_id);
			}
			
			// 有效期更新
			if(sProtocolsMgr.CreateClubPlayerInfoPacket(&packet, pChr))
				sGLMgr.SendProtocolsToChr(pChr, &packet);
		}
		else
		{// 费用不够
			result = enBuyPlayerRankResulr_NeedDiamand;
		}
	}
	
	if(sProtocolsMgr.CreateClubBuyPlayerRankResult(&packet, rank, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 升级俱乐部星级
bool CenterClubManager::PlayerBuyClubRank(CharPtr & pChr, const uint64 & club_id, const uint8 & rank)
{
	if(pChr.isNull() || club_id == 0 || rank == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	CenterClubMember ccMember;
	CenterClubConfig ccConfig, cpConfig;
	
WGS_CLUBROOM_LOCK
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enBuyClubRankResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enBuyClubRankResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		{// 不是公会成员
			result = enBuyClubRankResult_NotMember;
			break;
		}
		
		if(ccMember.type == enClubMemberType_Normal)
		{// 权限不足
			result = enBuyClubRankResult_NoQuanXian;
			break;
		}
		
		ccConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Club, rank, pUser->getUInt32Field("reg_from"));
		cpConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Player, pChr->getUInt32Field("rank"), pUser->getUInt32Field("reg_from"));
		if(ccConfig.config_id == 0)
		{// 未找到配置
			result = enBuyClubRankResult_NoConfig;
			break;
		}
		
		if(sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen) < ccConfig.pay_diamand)
		{// 费用不足
			result = enBuyClubRankResult_NeedDiamand;
			break;
		}
		
	}while(0);
	
	if(result == enBuyClubRankResult_Sucess)
	{
		if(!sGLMgr.reduceItemNum(pChr, ItemModel_DiamondOpen, ccConfig.pay_diamand))
		{// 扣钱失败
			result = enBuyClubRankResult_NeedDiamand;
		}
		else
		{// 扣钱成功
			if(rank > cc.rank)
			{// 星级提升
				cc.rank = rank;
				cc.max_member = ccConfig.max_member_add + cpConfig.max_member_add;
			}
			
			// 变更时间
			time_t expiry_time = sGLMgr.GetDateTimeSeconds(cc.expiry_date);
			Log.Debug("CenterClubManager::PlayerBuyClubRank","expiry_date[%s] expiry_time[%u] 111", cc.expiry_date.c_str(), expiry_time);
			if(expiry_time == 0)
			{
				expiry_time = time(0) + ccConfig.valid_days * 86400;
			}
			else
			{
				expiry_time += (ccConfig.valid_days * 86400);
			}
			
			cc.expiry_date = sGLMgr.GetCurDateTime(expiry_time);
			Log.Debug("CenterClubManager::PlayerBuyClubRank","expiry_date[%s] expiry_time[%u] 222", cc.expiry_date.c_str(), expiry_time);
			UpdateCenterClub(cc);
			
			WorldPacket packet;
			if(sProtocolsMgr.CreateClubNormalInfoPacket(&packet, cc))
				BroadcastClubPacket(&packet, cc.club_id);
		}
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubBuyClubRankResult(&packet, club_id, rank, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	return true;
}

// 操作玩家权限
bool CenterClubManager::ChangePlayerQuanXian(CharPtr & pChr, const uint64 & club_id, const uint32 & pid, const uint8 & member_type)
{
	if(pChr.isNull() || club_id == 0 || pid == 0)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	CenterClubConfig ccConfig;
	CenterClubMember srcMember, destMember;
	
WGS_CLUBROOM_LOCK
	uint32 src_pid = pUser->getUInt32Field("platform_id");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	uint8 org_memberType = 0;
	uint8 result = enClubOpMemberResult_Sucess;
	do
	{
		if(src_pid == pid)
		{// 不能操作自己
			result = enClubOpMemberResult_CannotOpSelf;
			break;
		}
		
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enClubOpMemberResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, src_pid, &srcMember))
		{// 不是公会成员
			result = enClubOpMemberResult_NoMember;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &destMember))
		{// 不是公会成员
			result = enClubOpMemberResult_NoMember;
			break;
		}
		
		if(destMember.type == member_type)
		{// 无效的操作
			result = enClubOpMemberResult_ErrorOp;
			break;
		}
		
		if(srcMember.type <= destMember.type)
		{// 权限不够
			result = enClubOpMemberResult_NoQuanXian;
			break;
		}
		
		ccConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Club, cc.rank, pUser->getUInt32Field("reg_from"));
		Log.Debug("CenterClubManager::ChangePlayerQuanXian","club_id[%u] rank[%u] config_id[%u] vice_president_add[%u]", 
							club_id, cc.rank, ccConfig.config_id, ccConfig.vice_president_add);
		if(member_type == enClubMemberType_VicePresident && cc.vice_president >= ccConfig.vice_president_add)
		{// 职位数量超上限
			result = enClubOpMemberResult_LimitZhiWei;
			break;
		}
		
		if(member_type != enClubMemberType_Normal && member_type != enClubMemberType_VicePresident)
		{// 权限不够
			result = enClubOpMemberResult_NoQuanXian;
			break;
		}
		
	}while(0);
	
	if(result == enClubOpMemberResult_Sucess)
	{
		org_memberType = destMember.type;
		
		// 更新被操作玩家公会成员数据
		destMember.type = member_type;
		sCenterClubMemberMgr.UpdateCenterClubMember(destMember);
		
		String strChangeMemberType = "";
		if(member_type == enClubMemberType_VicePresident)
		{
			if(agent_id == 5053)
				strChangeMemberType = "管理员";
			else
				strChangeMemberType = "副会长";
		}
		else if(member_type == enClubMemberType_Normal)
		{
			strChangeMemberType = "普通成员";
		}
		
		String strOpMemberType = "";
		if(srcMember.type == enClubMemberType_VicePresident)
		{
			if(agent_id == 5053)
				strOpMemberType = "管理员";
			else
				strOpMemberType = "副会长";
		}
		else if(srcMember.type == enClubMemberType_President)
		{
			if(agent_id == 5053)
				strOpMemberType = "创建者";
			else
				strOpMemberType = "会长";
		}
		
		// 权限变更消息
		char buf[256] = "";
		if(org_memberType > member_type)
			sprintf(buf, sMsg.getMessage("club_quanxian_lvdown").c_str(), destMember.nick.c_str(), strOpMemberType.c_str(), strChangeMemberType.c_str());
		else
			sprintf(buf, sMsg.getMessage("club_quanxian_lvup").c_str(), destMember.nick.c_str(), strOpMemberType.c_str(), strChangeMemberType.c_str());
		
		strChangeMemberType = buf;
		sCenterClubMessageMgr.AddSystemChatMessage(strChangeMemberType, cc.agent_id, cc.club_id);
		
		if(destMember.type == enClubMemberType_VicePresident)
		{// 副会长人数更新
			cc.vice_president++;
			UpdateCenterClub(cc);
		}
		else if(destMember.type == enClubMemberType_Normal)
		{// 降为普通成员
			cc.vice_president = cc.vice_president > 0 ? (--cc.vice_president) : 0;
			UpdateCenterClub(cc);
		}
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubChangeQuanXianResult(&packet, club_id, pid, member_type, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 踢出玩家
bool CenterClubManager::KickoutPlayer(CharPtr & pChr, const uint64 & club_id, const uint32 & pid)
{
	if(pChr.isNull() || club_id == 0 || pid == 0)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	CenterClubMember srcMember, destMember;
	
WGS_CLUBROOM_LOCK
	uint32 src_pid = pUser->getUInt32Field("platform_id");
	uint8 result = enClubOpMemberResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enClubOpMemberResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, src_pid, &srcMember))
		{// 不是公会成员
			result = enClubOpMemberResult_NoMember;
			break;
		}
		
		if(srcMember.type == enClubMemberType_Normal)
		{// 权限不够
			result = enClubOpMemberResult_NoQuanXian;
			break;
		}
		
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &destMember))
		{// 不是公会成员
			result = enClubOpMemberResult_NoMember;
			break;
		}
		
		if(srcMember.type <= destMember.type)
		{// 权限不够
			result = enClubOpMemberResult_NoQuanXian;
			break;
		}
		
	}while(0);
	
	if(result == enClubOpMemberResult_Sucess)
	{
		// 公会成员退出
		sCenterClubMemberMgr.ClubReduceMember(cc, pid);
		
		// 添加公会成员退出消息
		char buf[256] = "";
		if(cc.agent_id == 5053)
			sprintf(buf, sMsg.getMessage("club_kickout_playerjlb").c_str(), destMember.nick.c_str(), sGLMgr.GetCurDateTime().c_str());
		else
			sprintf(buf, sMsg.getMessage("club_kickout_player").c_str(), destMember.nick.c_str(), sGLMgr.GetCurDateTime().c_str());
		
		String strMessage = buf;
		sCenterClubMessageMgr.AddSystemChatMessage(strMessage, cc.agent_id, cc.club_id);
	}
	
	// 发送结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubKickOutResult(&packet, club_id, pid, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 俱乐部聊天
bool CenterClubManager::PlayerChat(CharPtr & pChr, const uint64 & club_id, String & content)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint8 result = enClubChatResult_Sucess;
	do
	{
		if(content.size() > 255)
		{// 消息内容过长
			result = enClubChatResult_TooLong;
			break;
		}
		
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enClubChatResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.IsPlayerInClub(club_id, pid))
		{// 不是成员
			result = enClubChatResult_NotMember;
			break;
		}
		
	}while(0);
	
	if(result == enClubChatResult_Sucess)
	{
		// 添加聊天消息
		uint64 message_id = sCenterClubMessageMgr.AddChatMessage(pChr, content, club_id);
		
		CenterClubMessage message;
		sCenterClubMessageMgr.GetCenterClubMessage(message_id, &message);
		
		// 广播公会成员
		WorldPacket packet;
		if(sProtocolsMgr.CreateClubChatInfoPacket(&packet, club_id, message))
			BroadcastClubPacket(&packet, club_id);
		
		return true;
	}
	
	return false;
}

// 广播公会所有成员
bool CenterClubManager::BroadcastClubPacket(WorldPacket * packet, const uint64 & club_id)
{
	if(NULL == packet)
		return false;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return false;
	
	std::list<CenterClubMember> lstClubMember;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstClubMember, "status < 255 and club_id = %u", club_id);
	if(lstClubMember.empty())
		return false;
	
	uint32 gz_id = cc.gz_id;
	std::list<CenterClubMember>::iterator iter, ei = lstClubMember.end();
	for(iter = lstClubMember.begin(); iter!=ei; ++iter)
	{
		sGLMgr.SendProtocolsToChrByPidGzid((*iter).pid, gz_id, packet);
	}
	
	return true;
}

// 获得发现的俱乐部房间列表
bool CenterClubManager::GetMyClubRoomList(CharPtr & pChr)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	// 获得自己参与的俱乐部列表
	std::list<CenterClub> lstClub;
	std::list<CenterClubMember> lstMember;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstMember, "status < 255 and pid = %u", pUser->getUInt32Field("platform_id"));
	
	std::list<CenterClubMember>::iterator iter, ei = lstMember.end();
	for(iter = lstMember.begin(); iter!=ei; ++iter)
	{
		CenterClub cc;
		if(GetCenterClub((*iter).club_id, &cc))
			lstClub.push_back(cc);
	}
	
	// 获得俱乐部组局列表
	std::list<CenterClubRoom> lstRoomList;
	std::list<CenterClub>::iterator itClub, eiClub = lstClub.end();
	for(itClub = lstClub.begin(); itClub!=eiClub; ++itClub)
	{
		std::list<CenterClubRoom> lstData;
		sCenterClubRoomMgr.GetCenterClubRoomList(&lstData, (*itClub).club_id);
		if(lstData.size())
		{
			lstRoomList.insert(lstRoomList.end(), lstData.begin(), lstData.end());
		}
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubRoomListPacket(&packet, 0, 0, lstRoomList))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 购买商品
bool CenterClubManager::BuyClubGoods(CharPtr & pChr, const uint32 & goods_id)
{
	if(pChr.isNull() || goods_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
WGS_CLUBROOM_LOCK
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	CenterClubConfig ccConfig;
	uint8 result = enClubBuyGoodsResult_Sucess;
	do
	{
		ccConfig = sCenterClubConfigMgr.GetCenterClubGoodsConfig(goods_id, agent_id);
		if(ccConfig.config_id == 0)
		{// 错误的商品ID
			result = enClubBuyGoodsResult_ErrorGoodsId;
			break;
		}
		
		if(ccConfig.pay_diamand == 0 || ccConfig.give_coins == 0)
		{// 错误的商品ID
			result = enClubBuyGoodsResult_ErrorGoodsId;
			break;
		}
		
		if(sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen) < ccConfig.pay_diamand)
		{// 身上钻石不足
			result = enClubBuyGoodsResult_NeedDiamand;
			break;
		}
		
	}while(0);
	
	if(result == enClubBuyGoodsResult_Sucess)
	{
		// 扣除钻石
		if(sGLMgr.reduceItemNum(pChr, ItemModel_DiamondOpen, ccConfig.pay_diamand))
		{
			// 添加铜钱
			sGLMgr.addItemNum(pChr, ItemModel_Coins, ccConfig.give_coins);
			// 记录日志
			sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubBuyGoods, ItemModel_DiamondOpen, en_UNT_Reduce, ccConfig.pay_diamand, ccConfig.config_id);
			sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubBuyGoods, ItemModel_Coins, en_UNT_Add, ccConfig.give_coins, ccConfig.config_id);
		}
		else
		{
			result = enClubBuyGoodsResult_NeedDiamand;
		}
	}
	
	// 发送购买结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateCLubBuyGoodsResultPacket(&packet, goods_id, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 解散公会
bool CenterClubManager::DissolveClub(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull() || !club_id)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	uint32 pid = pUser->getUInt32Field("platform_id");
	
WGS_CLUBROOM_LOCK
	uint8 result = enClubDissolveResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enClubDissolveResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.IsPlayerClubPresident(club_id, pid))
		{// 不是公会会长
			result = enClubDissolveResult_NotPresident;
			break;
		}
		
	}while(0);
	
	WorldPacket packet;
	if(result == enClubDissolveResult_Sucess)
	{
		// 广播公会解散
		if(sProtocolsMgr.CreateClubDissolvePacket(&packet, club_id, result))
			BroadcastClubPacket(&packet, club_id);
		
		// 公会成员退出
		sCenterClubMemberMgr.ClubClearAllMembrt(cc);
		
		// 处理加入审核消息，全部设置为无效
		Fields fields;
		fields["status"] = 255;
		
		sCenterClubMessageMgr.UpdateCenterClubMessage(&fields, "club_id = %u and status < 255 and type = %u", club_id, enReviewMessageType_Join);
		
		// 处理带入审核消息
		std::list<CenterClubMessage> lstClubMessage;
		sCenterClubMessageMgr.GetCenterClubMessageList(&lstClubMessage, "club_id = %u and status < 255 and type = %u", club_id, enReviewMessageType_Takein);
		
		std::list<CenterClubMessage>::iterator iter, ei = lstClubMessage.end();
		for(iter = lstClubMessage.begin(); iter!=ei; ++iter)
		{
			CharPtr pMemberChar = sCharMgr.load((*iter).owner_cid);
			if(pMemberChar.isNull())
				continue;
			
			// 退钱
			sGLMgr.addItemNum(pChr, ItemModel_Coins, (*iter).takein_coins);
			// 记录日志
			sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubTakeinCoins, ItemModel_Coins, en_UNT_Add, (*iter).takein_coins, cc.club_id);	
			(*iter).status = 255;
			sCenterClubMessageMgr.UpdateCenterClubMessage(*iter);
			
			uint32 pid = 0;
			UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
			if(!pUser.isNull())
				pid = pUser->getUInt32Field("platform_id");
			
			// 带入消息的退分邮件
			std::map<uint32, uint32> mapItems;
			const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_JIESAN_DAIRUSHENHE");
			if(pModel)
			{
				String szTitle = pModel->szTitle;
				String szContent = pModel->szContent;
				// 俱乐部【%s】解散了，现返回您带入消息中的%u游戏币。
				
				char buf[256] = "";
				sprintf(buf, szContent.c_str(), cc.name.c_str(), (*iter).takein_coins);
				sGLMgr.CreateMail(pid, szTitle, buf, 0, 0, &mapItems);				
			}
		}
		
	}
	else
	{// 发送结果
		if(sProtocolsMgr.CreateClubDissolvePacket(&packet, club_id, result))
			sGLMgr.SendProtocolsToChr(pChr, &packet);
	}
	
	return true;
}

// 修改公会信息
bool CenterClubManager::ChangeClubInfo(CharPtr & pChr, const uint32 & club_id, const uint32 & type, String & strData)
{
	if(pChr.isNull() || !type)
		return false;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	uint32 pid = pUser->getUInt32Field("platform_id");
	
	uint8 result = enClubInfoChangeResult_Sucess;
	do
	{
		if(!GetCenterClub(club_id, &cc))
		{// 公会不存在
			result = enClubInfoChangeResult_NoClub;
			break;
		}
		
		if(!sCenterClubMemberMgr.IsPlayerClubManager(club_id, pid))
		{// 不是公会管理员
			result = enClubInfoChangeResult_NotManager;
			break;
		}
		
		if(type != 1 && type != 2)
		{// 无效的操作
			result = enClubInfoChangeResult_ErrorType;
			break;
		}
		
	}while(0);
	
	
	if(result == enClubDissolveResult_Sucess)
	{
		if(type == 1)
		{// 修改简介
			cc.desc = strData;
		}
		else
		{// 修改公告
			cc.notice = strData;
		}
		
		// 更新公会信息
		UpdateCenterClub(cc);
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubChangeInfoResultPacket(&packet, club_id, type, strData, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 增加魅力值
bool CenterClubManager::AddClubPlayerCharm(CharPtr & pChr, const uint32 & num)
{
	if(pChr.isNull() || num == 0)
		return false;
	
	AICreatureBaseInterface * pChrBaseAI = TO_CREATURE_BASE_INTERFACE(pChr->getAIInterface());
	if(!pChrBaseAI)
		return false;
	
	pChrBaseAI->AddXMLValue("CLUB_CHARM", num);
	return true;
}

// 增加活跃值
bool CenterClubManager::AddClubActivityValue(const uint64 & club_id, const uint64 & count)
{
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return false;
	
	cc.activity_value += count;
	UpdateCenterClub(cc);
	
	sCenterClubLogMgr.AddActivityChangeLog(cc, count);
	return true;
}

// 获取玩家详细信息
bool CenterClubManager::GetClubPlayerDetails(CharPtr & pChr, const uint32 & dest_pid)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	uint32 gz_id = pChr->getUInt32Field("gz_id");
	
	std::list<ResourcePtr> lstUser;
	sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", dest_pid, gz_id);
	if(lstUser.empty())
		return false;
	
	ResourcePtr pDestUser = *lstUser.begin();
	if(pDestUser.isNull())
		return false;
	
	CharPtr pDestChr = sCharMgr.load(pDestUser->getUInt32Field("char_id"));
	if(pDestChr.isNull())
		return false;
	
	CenterClubRemark ccRemark;
	sCenterClubRemarkMgr.GetCenterClubRemark(pUser->getUInt32Field("platform_id"), pDestUser->getUInt32Field("platform_id"), &ccRemark);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubPlayerDetailsPacket(&packet, pDestChr, ccRemark.remark_info))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	return true;
}

// 获得俱乐部活动经费
uint32 CenterClubManager::GetClubActivityValue(const uint64 & club_id)
{
	if(club_id == 0)
		return 0;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return 0;
	
	return cc.activity_value;
}

// 增加俱乐部活动经费
bool CenterClubManager::AddClubActivityValue(const uint64 & club_id, const uint32 & num)
{
	if(club_id == 0 || num == 0)
		return false;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return 0;
	
	cc.activity_value += num;
	return UpdateCenterClub(cc);
}

// 减少俱乐部活动经费
bool CenterClubManager::ReduceClubActivityValue(const uint64 & club_id, const uint32 & num)
{
	if(club_id == 0 || num == 0)
		return false;
	
	CenterClub cc;
	if(!GetCenterClub(club_id, &cc))
		return false;
	
	Json::Value val;
	val["pid"] = cc.owner_pid;
	val["model_id"] = ItemModel_DiamondOpen;
	val["num"] = num;
	val["club_id"] = uint32(cc.club_id);
	val["agent_id"] = cc.agent_id;
	
	Json::FastWriter w;
	if(!sGLMgr.OperatorItemFromGatewayToPlayer(w.write(val)))
		return false;
	
	return true;
}

// 根据玩家更新俱乐部经费
bool CenterClubManager::UpdateClubActivityValueByCraeter(CharPtr & pChr)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	uint32 pid = pUser->getUInt32Field("platform_id");
	
	// 目前只有枪王俱乐部和满天飞俱乐部有该逻辑
	if(agent_id != 5053 && agent_id != 5025)
		return false;
	
	uint32 create_num = PlayerCreateClubCount(pChr);
	if(create_num == 0)
		return false;
	
	uint32 curr_diamand = sGLMgr.GetItemNum(pChr, ItemModel_DiamondOpen);
	
	Fields fields;
	fields["activity_value"] = curr_diamand;
	
	return UpdateCenterClub(&fields, "status < 255 and agent_id = %u and owner_pid = %u", agent_id, pid);
}

// 获取俱乐部创建者PID
uint32 CenterClubManager::GetClubCreaterPid(const uint32 & club_id)
{
	if(club_id == 0)
		return 0;
	
	CenterClub cc;
	GetCenterClub(club_id, &cc);
	
	return cc.owner_pid;
}

// 创建快捷开房配置
bool CenterClubManager::CreateFastCreateMode(CharPtr & pChr, const uint32 & club_id, String & strConfig)
{
	if(pChr.isNull() || club_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	// 不是创建者
	uint32 pid = pUser->getUInt32Field("platform_id");
	if(!sCenterClubMemberMgr.IsPlayerClubManager(club_id, pid))
		return false;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return false;
	
	String strConfigMD5;
	
	Json::Value val;
	Json::Reader reader;
	
	uint8 result = enClubCreateFastMode_Sucess;
	do
	{
		
		reader.parse(cc.config_data, val);
		
		uint32 max_count = sXmlDataMgr.GetConfXMLValue("FAST_CREATE_COUNT");
		if(max_count <= val.size())
		{
			result = enClubCreateFastMode_Max;
			break;
		}
		
		if(val.size())
		{
			CMD5 md5;
			strConfigMD5 = md5.GenerateMD5(strConfig.c_str());
			
			for (Json::Value::iterator iterVal = val.begin(); iterVal != val.end(); ++iterVal)
			{
				String strIterConfig = (*iterVal).asString();
				String strIterConfigMD5 = md5.GenerateMD5(strIterConfig);
				
				if(!strConfigMD5.compare(strIterConfigMD5))
				{// 相同配置
					result = enClubCreateFastMode_Same;
					break;
				}
			}
		}
		
	}while(0);
	
	// 发送添加结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubFastModeCreatePacket(&packet, club_id, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enClubCreateFastMode_Sucess)
	{
		val[val.size()] = strConfig;
		
		Json::FastWriter w;
		cc.config_data = w.write(val);
		
		// 发送快捷配置列表
		// if(sProtocolsMgr.CreateClubFastModeListPacket(&packet, club_id, val))
			// sGLMgr.SendProtocolsToChr(pChr, &packet);
		
		return UpdateCenterClub(cc);
	}
	
	return true;
}

// 移除快捷开放配置
bool CenterClubManager::DeleteFastCreateMode(CharPtr & pChr, const uint32 & club_id, const uint8 & del_index)
{
	if(pChr.isNull() || club_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	// 不是创建者
	uint32 pid = pUser->getUInt32Field("platform_id");
	if(!sCenterClubMemberMgr.IsPlayerClubManager(club_id, pid))
		return false;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return false;
	
	Json::Value val, valNew;
	Json::Reader reader;
	reader.parse(cc.config_data, val);
	
	uint8 result = enClubDeleteFastMode_Sucess;
	do
	{
		if(val.size() - 1 < del_index)
		{// 无效的索引
			result = enClubDeleteFastMode_ErrorIndex;
			break;
		}
		
		bool bDelete = false;
		uint8 curr_index = 0;
		uint8 val_index = 0;
		for (Json::Value::iterator iterVal = val.begin(); iterVal != val.end(); ++iterVal)
		{
			++curr_index;
			if(curr_index == del_index + 1)
			{
				bDelete = true;
				continue;
			}
			
			valNew[val_index++] = *iterVal;
		}
		
		if(!bDelete)
		{// 无效的索引
			result = enClubDeleteFastMode_ErrorIndex;
			break;
		}
		
	}while(0);
	
	// 发送删除结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubFastModeDeletePacket(&packet, club_id, del_index, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enClubDeleteFastMode_Sucess)
	{// 发送删除后的快捷配置列表
		if(valNew.size())
		{
			Json::FastWriter w;
			cc.config_data = w.write(valNew);
		}
		else
		{
			cc.config_data = "";
		}
		
		return UpdateCenterClub(cc);
		// 发送快捷配置列表
		// if(sProtocolsMgr.CreateClubFastModeListPacket(&packet, club_id, valNew))
			// sGLMgr.SendProtocolsToChr(pChr, &packet);
	}
	
	return true;
}

// 获得快捷开房配置
bool CenterClubManager::GetFastBuildMode(CharPtr & pChr, const uint32 & club_id)
{
	if(pChr.isNull() || club_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	// 不是公会成员
	uint32 pid = pUser->getUInt32Field("platform_id");
	if(!sCenterClubMemberMgr.IsPlayerInClub(club_id, pid))
		return false;	
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return false;
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(cc.config_data, val);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubFastModeListPacket(&packet, club_id, val))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 永久封停俱乐部
bool CenterClubManager::ManagerStopClub(const uint32 & show_id)
{
	if(!show_id)
		return false;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClubByShowID(show_id, &cc))
		return false;
	
	cc.status = 255;
	return sCenterClubMgr.UpdateCenterClub(cc);
}

//===========================俱乐部逻辑相关==================================


//===========================俱乐部成员相关==================================
//--------------------------------------------------------
bool CenterClubMemberManager::AddCenterClubMember(CenterClubMember & cc)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &fields);
	
	// uint32 member_id;						// 成员ID
	fields["status"] = cc.status;				// 状态
	fields["agent_id"] = cc.agent_id;			// 代理商ID
	fields["gz_id"] = cc.gz_id;					// 分区ID
	fields["club_id"] = cc.club_id;				// 俱乐部ID
	fields["type"] = cc.type;					// 类型
	fields["pid"] = cc.pid;						// 玩家PID
	fields["play_loop"] = cc.play_loop;			// 游戏局数
	fields["takein_coins"] = cc.takein_coins;	// 带入分数
	fields["curr_score"] = cc.curr_score;		// 当前分数
	fields["curr_result"] = cc.curr_result;		// 当前成绩
	fields["total_pump"] = cc.total_pump;		// 总抽水
	
	fields["pump_info"] = cc.pump_info;			// 抽水详情
	
	fields["nick"] = cc.nick;					// 昵称
	fields["icon_url"] = cc.icon_url;			// 头像
	fields["addr"] = cc.addr;					// 物理地址
	
	fields["referee_pid"] = cc.referee_pid;		// 推荐人PID
	fields["referee_nick"] = cc.referee_nick;	// 推荐人昵称
	fields["referee_icon"] = cc.referee_icon;	// 推荐人头像
	
	fields["create_time"] = cc.create_time;		// 创建时间
	fields["exit_time"] = cc.exit_time;			// 退出时间
	fields["ll_time"] = cc.ll_time;				// 最后一次登录时间
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &fields))
		return false;
	
	cc.member_id = fields["member_id"].GetUInt64();
	return true;
}

//--------------------------------------------------------
bool CenterClubMemberManager::UpdateCenterClubMember(const CenterClubMember & cc)
{
	Fields fields;
	fields["status"] = cc.status;
	// fields["m_agent_id"] = cc.m_agent_id;
	fields["gz_id"] = cc.gz_id;
	fields["club_id"] = cc.club_id;
	fields["type"] = cc.type;
	fields["pid"] = cc.pid;
	fields["play_loop"] = cc.play_loop;
	fields["takein_coins"] = cc.takein_coins;
	fields["curr_score"] = cc.curr_score;
	fields["curr_result"] = cc.curr_result;
	fields["total_pump"] = cc.total_pump;
	
	fields["pump_info"] = cc.pump_info;
	
	fields["nick"] = cc.nick;
	fields["icon_url"] = cc.icon_url;
	fields["addr"] = cc.addr;
	
	fields["referee_pid"] = cc.referee_pid;
	fields["referee_nick"] = cc.referee_nick;
	fields["referee_icon"] = cc.referee_icon;
	
	fields["create_time"] = cc.create_time;
	fields["exit_time"] = cc.exit_time;
	fields["ll_time"] = cc.ll_time;
	
	Log.Debug("CenterClubMemberManager::UpdateCenterClubMember","member_id[%u] club_id[%u] curr_score[%u] curr_result[%u]", cc.member_id, cc.club_id, cc.curr_score, cc.curr_result);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &fields, "`member_id` = %u", cc.member_id);
	return count > 0;
}

//--------------------------------------------------------
bool CenterClubMemberManager::UpdateCenterClubMember(Fields * fields, const char * QueryString, ...)
{
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, QueryString); 
	vsnprintf(sql, SQLBUFER_LEN, QueryString, vlist); 
	va_end(vlist);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, fields, sql);
	return count > 0;	
}

//--------------------------------------------------------
bool CenterClubMemberManager::UpdateCenterClubMember(const uint64 &ccMember_id, Fields * fields)
{
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, fields, "`member_id` = %u", ccMember_id);
	return count > 0;	
}

//--------------------------------------------------------
bool CenterClubMemberManager::GetCenterClubMember(const uint64 &ccMember_id, CenterClubMember * cc)
{
	assert (cc);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &rows, "`status` < 255 and `member_id` = %u", ccMember_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	
	cc->member_id = fields["member_id"].GetUInt32();
	cc->status = fields["status"].GetUInt32();
	cc->gz_id = fields["gz_id"].GetUInt32();
	cc->agent_id = fields["agent_id"].GetUInt32();
	cc->club_id = fields["club_id"].GetUInt64();
	cc->type = fields["type"].GetUInt32();
	cc->pid = fields["pid"].GetUInt32();
	
	cc->play_loop = fields["play_loop"].GetUInt32();
	cc->takein_coins = fields["takein_coins"].GetUInt32();
	cc->curr_score = fields["curr_score"].GetUInt32();
	cc->curr_result = fields["curr_result"].GetUInt32();
	cc->total_pump = fields["total_pump"].GetUInt32();
	
	cc->pump_info = fields["pump_info"].GetValue();
	cc->nick = fields["nick"].GetValue();
	cc->icon_url = fields["icon_url"].GetValue();
	cc->addr = fields["addr"].GetValue();
	
	cc->referee_pid = fields["referee_pid"].GetUInt32();
	cc->referee_nick = fields["referee_nick"].GetValue();
	cc->referee_icon = fields["referee_icon"].GetValue();
	
	cc->create_time = fields["create_time"].GetValue();
	cc->exit_time = fields["exit_time"].GetValue();
	cc->ll_time = fields["ll_time"].GetValue();
	return true;
}

bool CenterClubMemberManager::GetCenterClubMember(const uint64 & club_id, const uint32 & pid, CenterClubMember * ccMember)
{
	assert (ccMember);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &rows, "`status` < 255 and `club_id` = %u and `pid` = %u", club_id, pid);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	
	ccMember->member_id = fields["member_id"].GetUInt32();
	ccMember->status = fields["status"].GetUInt32();
	ccMember->gz_id = fields["gz_id"].GetUInt32();
	ccMember->agent_id = fields["agent_id"].GetUInt32();
	ccMember->club_id = fields["club_id"].GetUInt64();
	ccMember->type = fields["type"].GetUInt32();
	ccMember->pid = fields["pid"].GetUInt32();
	
	ccMember->play_loop = fields["play_loop"].GetUInt32();
	ccMember->takein_coins = fields["takein_coins"].GetUInt32();
	ccMember->curr_score = fields["curr_score"].GetUInt32();
	ccMember->curr_result = fields["curr_result"].GetUInt32();
	ccMember->total_pump = fields["total_pump"].GetUInt32();
	
	ccMember->pump_info = fields["pump_info"].GetValue();
	ccMember->nick = fields["nick"].GetValue();
	ccMember->icon_url = fields["icon_url"].GetValue();
	ccMember->addr = fields["addr"].GetValue();
	
	ccMember->referee_pid = fields["referee_pid"].GetUInt32();
	ccMember->referee_nick = fields["referee_nick"].GetValue();
	ccMember->referee_icon = fields["referee_icon"].GetValue();
	
	ccMember->create_time = fields["create_time"].GetValue();
	ccMember->exit_time = fields["exit_time"].GetValue();
	ccMember->ll_time = fields["ll_time"].GetValue();
	return true;
}

//--------------------------------------------------------
uint32 CenterClubMemberManager::GetCenterClubMemberList(std::list<CenterClubMember> * lstClubMember, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstClubMember->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubMember cc;
		Fields &fields = *iter;
		
		cc.member_id = fields["member_id"].GetUInt32();
		cc.status = fields["status"].GetUInt32();
		cc.agent_id = fields["agent_id"].GetUInt32();
		cc.gz_id = fields["gz_id"].GetUInt32();
		cc.club_id = fields["club_id"].GetUInt64();
		cc.type = fields["type"].GetUInt32();
		cc.pid = fields["pid"].GetUInt32();
		
		cc.play_loop = fields["play_loop"].GetUInt32();
		cc.takein_coins = fields["takein_coins"].GetUInt32();
		cc.curr_score = fields["curr_score"].GetUInt32();
		cc.curr_result = fields["curr_result"].GetUInt32();
		cc.total_pump = fields["total_pump"].GetUInt32();
		
		cc.pump_info = fields["pump_info"].GetValue();
		cc.nick = fields["nick"].GetValue();
		cc.icon_url = fields["icon_url"].GetValue();
		cc.addr = fields["addr"].GetValue();
		
		cc.referee_pid = fields["referee_pid"].GetUInt32();
		cc.referee_nick = fields["referee_nick"].GetValue();
		cc.referee_icon = fields["referee_icon"].GetValue();
		
		cc.create_time = fields["create_time"].GetValue();
		cc.exit_time = fields["exit_time"].GetValue();
		cc.ll_time = fields["ll_time"].GetValue();
		
		lstClubMember->push_back(cc);
	}
	
	return lstClubMember->size();
}

//--------------------------------------------------------
uint32 CenterClubMemberManager::GetTargetClubMemberList(std::list<CenterClubMember> * lstClubMember, const uint64 & club_id)
{
	lstClubMember->clear();
	
	GetCenterClubMemberList(lstClubMember, "status < 255 and club_id = %u order by ll_time desc", club_id);
	return lstClubMember->size();
}

// 查询结果集数量
uint32 CenterClubMemberManager::GetCenterClubMemberDBCount(const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	FieldRows fieldRows;
	sDataMgr.queryDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMEMBER, &fieldRows, "count(`member_id`) as `count`", sql);
	
	uint32 total_num = 0;
	if(fieldRows.size())
	{
		std::map<String,Field>::iterator itMap, eiMap = fieldRows[0].end();
		for(itMap = fieldRows[0].begin(); itMap!=eiMap; ++itMap)
		{
			total_num += (itMap->second).GetUInt32();
		}
	}
	
	return total_num;
}

// 更新玩家会员最后登录时间
void CenterClubMemberManager::UpdateMemberOnlineLastLoginTime(const uint32 & pid)
{
	Fields fields;
	fields["status"] = 1;								// 在线
	fields["ll_time"] = sGLMgr.GetCurDateTime();		// 最后登录时间
	
	UpdateCenterClubMember(&fields, "status < 255 and pid = %u", pid);
}

// 更新玩家会员离线状态
void CenterClubMemberManager::UpdateMemberOffline(const uint32 & pid)
{
	Fields fields;
	fields["status"] = 2;								// 离线
	fields["ll_time"] = sGLMgr.GetCurDateTime();		// 最后登录时间
	
	UpdateCenterClubMember(&fields, "status < 255 and pid = %u", pid);
}

// 玩家是否在公会中
bool CenterClubMemberManager::IsPlayerInClub(const uint64 & club_id, const uint32 & pid)
{
	return GetCenterClubMemberDBCount("status < 255 and club_id = %u and pid = %u", club_id, pid);
}

// 公会增加成员
bool CenterClubMemberManager::ClubAddMember(CenterClub & cc, const uint32 & pid, const uint32 & referee_id, const uint32 & member_type)
{
	if(cc.club_id == 0 || pid == 0)
		return false;
	
	std::list<ResourcePtr> lstUser;
	sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", pid, cc.gz_id);
	if(lstUser.empty())
		return false;
	
	UserPtr pUser = sUserMgr.load((*lstUser.begin())->getUInt32Field("uid"));
	if(pUser.isNull())
		return false;
	
	// 改为DBOBJECTS的方式
	CharPtr pChr = sCharMgr.load(pUser->getUInt32Field("char_id"));
	if(pChr.isNull())
		return false;
	
	UserPtr pRefereeUser;
	CharPtr pRefereeChr;
	
	if(referee_id)
	{
		sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", referee_id, cc.gz_id);
		if(lstUser.empty())
			return false;
		
		pRefereeUser = sUserMgr.load((*lstUser.begin())->getUInt32Field("uid"));
		if(pRefereeUser.isNull())
			return false;
		
		pRefereeChr = sCharMgr.load(pRefereeUser->getUInt32Field("char_id"));
		if(pRefereeChr.isNull())
			return false;
	}
	
	CenterClubMember ccMember;
	
	ccMember.status = 1;
	ccMember.agent_id = pUser->getUInt32Field("reg_from");		// 代理商ID
	ccMember.gz_id = pUser->getUInt32Field("gz_id");			// 分区ID
	ccMember.club_id = cc.club_id;								// 俱乐部ID
	ccMember.type = member_type;								// 类型（0,会员 1 副会长 2 会长）
	ccMember.pid = pUser->getUInt32Field("platform_id");		// 玩家平台ID
	ccMember.play_loop = 0;										// 游戏局数
	ccMember.takein_coins = 0;									// 带入分数
	ccMember.curr_score = 0;									// 当前分数
	
	ccMember.nick = pChr->getStringField("name");				// 昵称
	ccMember.icon_url = pChr->getStringField("desc");			// 头像地址
	ccMember.addr = pUser->getStringField("addr");				// 物理地址
	
	if(!pRefereeUser.isNull() && !pRefereeChr.isNull())
	{
		ccMember.referee_pid = pRefereeUser->getUInt32Field("platform_id");		// 推荐人PID
		ccMember.referee_nick = pRefereeChr->getStringField("name");			// 推荐人昵称
		ccMember.referee_icon = pRefereeChr->getStringField("desc");			// 推荐人头像
	}
	
	ccMember.create_time = sGLMgr.GetCurDateTime();					// 创建时间
	ccMember.ll_time = ccMember.create_time;						// 最后一次登录时间
	
	AddCenterClubMember(ccMember);
	
	cc.curr_member++;
	sCenterClubMgr.UpdateCenterClub(cc);							// 更新俱乐部
	return true;
}

// 公会减少成员
bool CenterClubMemberManager::ClubReduceMember(CenterClub & cc, const uint32 & pid, bool bJieSan)
{
	if(cc.club_id == 0 || pid == 0)
		return false;
	
	CenterClubMember ccMember;
	if(!GetCenterClubMember(cc.club_id, pid, &ccMember))
		return false;
	
	if(ccMember.curr_score)
	{// 退钱给玩家
		std::list<ResourcePtr> lstUser;
		sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", pid, cc.gz_id);
		if(!lstUser.empty())
		{
			UserPtr pUser = sUserMgr.load((*lstUser.begin())->getUInt32Field("uid"));
			if(!pUser.isNull())
			{
				CharPtr pChr = sCharMgr.load(pUser->getUInt32Field("char_id"));
				if(!pChr.isNull())
				{
					uint32 real_score = ccMember.curr_score * 1.1f;
					// 加钱
					sGLMgr.addItemNum(pChr, ItemModel_Coins, real_score);
					// 记录日志
					sLogDataMgr.UpdateItemsInfoLog(pChr, en_ST_ClubReturnCoins, ItemModel_Coins, en_UNT_Add, real_score, cc.club_id);
					// 添加退分历史
					sCenterClubLogMgr.AddExitCoinsLog(ccMember);
					
					if(bJieSan)
					{// 发送邮件通知
						std::map<uint32, uint32> mapItems;
						const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_JIESAN_TUIFEN");
						if(pModel)
						{
							String szTitle = pModel->szTitle;
							String szContent = pModel->szContent;
							// 俱乐部【%s】解散，返回你的游戏币%u。
							
							char buf[256] = "";
							sprintf(buf, szContent.c_str(), cc.name.c_str(), ccMember.curr_score);
							sGLMgr.CreateMail(pid, szTitle, buf, 0, 0, &mapItems);				
						}
					}
					
				}
			}
		}
	}
	
	ccMember.status = 255;
	UpdateCenterClubMember(ccMember);
	
	if(cc.curr_member)
	{// 更新公会人数
		cc.curr_member--;
		if(ccMember.type == enClubMemberType_VicePresident)
		{// 踢出的是副会长，减少会员数量
			cc.vice_president = cc.vice_president > 0 ? --cc.vice_president : 0;
		}
		sCenterClubMgr.UpdateCenterClub(cc);
	}
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubPlayerExitResult(&packet, ccMember.club_id, enPlayerExitClubResult_Sucess))
		sGLMgr.SendProtocolsToChrByPidGzid(pid, cc.gz_id, &packet);
	return true;
}

// 解散公会
bool CenterClubMemberManager::ClubClearAllMembrt(CenterClub & cc)
{
	if(cc.club_id == 0)
		return false;
	
	std::list<CenterClubMember> lstMember;
	if(!GetCenterClubMemberList(&lstMember, "status < 255 and club_id = %u", cc.club_id))
		return false;
	
	// 挨个踢出公会，处理他们带入等
	std::list<CenterClubMember>::iterator iter, ei = lstMember.end();
	for(iter = lstMember.begin(); iter!=ei; ++iter)
	{
		ClubReduceMember(cc, (*iter).pid, true);
	}
	
	// 公会状态更新
	cc.curr_member = 0;
	cc.status = 255;
	sCenterClubMgr.UpdateCenterClub(cc);
	return true;
}

// 玩家是否公会管理员
bool CenterClubMemberManager::IsPlayerClubManager(const uint64 & club_id, const uint32 & pid)
{
	if(!club_id || !pid)
		return false;
	
	CenterClubMember ccMember;
	if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		return false;
	
	return ccMember.type >= enClubMemberType_VicePresident;
}

// 玩家是否公会会长
bool CenterClubMemberManager::IsPlayerClubPresident(const uint64 & club_id, const uint32 & pid)
{
	if(!club_id || !pid)
		return false;
	
	CenterClubMember ccMember;
	if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		return false;
	
	return ccMember.type == enClubMemberType_President;
}

// 获取玩家成员列表（成绩排序）
bool CenterClubMemberManager::GetMemberListByChengji(CharPtr & pChr, const uint64 & club_id)
{
	if(!club_id)
		return false;
	
	std::list<CenterClubMember> lstClubMember;
	GetTargetClubMemberList(&lstClubMember, club_id);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubChengjiOrderPacket(&packet, club_id, lstClubMember))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	return true;
}

// 成员完成一局游戏
bool CenterClubMemberManager::MemberComplateGame(const uint64 & club_id, const uint32 & pid, const int32 & winLose, const uint32 & pump_coins)
{
	if(!club_id || !pid)
		return false;
	
WGS_CLUBMEMBER_LOCK
	CenterClubMember ccMember;
	if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		return false;
	
	int32 real_winLose = winLose;
	if(real_winLose > 0 && pump_coins > 0)
		real_winLose = real_winLose > pump_coins ? real_winLose - pump_coins : 0;
	
	++ccMember.play_loop;
	
	// 玩家成绩 = 当前积分 - 带入金额
	ccMember.curr_result = (int32(ccMember.curr_score) - int32(ccMember.takein_coins));
	ccMember.total_pump += pump_coins;
	
	Log.Debug("CenterClubMemberManager::MemberComplateGame","club_id[%u] pid[%u] curr_result[%d] curr_score[%d] takein_coins[%u] ccMember.total_pump[%u]", club_id, pid, ccMember.curr_result, ccMember.curr_score, ccMember.takein_coins, ccMember.total_pump);
	return UpdateCenterClubMember(ccMember);
}

// 获得玩家某个公会的带入金额
uint32 CenterClubMemberManager::GetMemberTakeinCoins(const uint32 & club_id, const uint32 & pid)
{
	if(!club_id || !pid)
		return 0;
	
WGS_CLUBMEMBER_LOCK
	CenterClubMember ccMember;
	if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		return false;
	
	return ccMember.takein_coins;
}

// 广播消息给管理员
void CenterClubMemberManager::BroadcastClubManagerMemberPacket(const uint64 & club_id, WorldPacket * packet)
{
	// 给管路员发有新的审核消息数量
	std::list<CenterClubMember> lstClubMember;
	sCenterClubMemberMgr.GetCenterClubMemberList(&lstClubMember, "status < 255 and club_id = %u and type > %u", club_id, enClubMemberType_Normal);
	
	std::list<CenterClubMember>::iterator iter, ei = lstClubMember.end();
	for(iter = lstClubMember.begin(); iter!=ei; ++iter)
	{
		sGLMgr.SendProtocolsToChrByPidGzid((*iter).pid, (*iter).gz_id, packet);
	}
}

// 获得玩家某个公会的当前金额
uint32 CenterClubMemberManager::GetMemberCurrCoins(const uint32 & club_id, CharPtr & pChr)
{
	if(!club_id || pChr.isNull())
		return 0;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return 0;
	
	if(pUser->getUInt32Field("reg_from") == 5045 || pUser->getUInt32Field("reg_from") == 5046)
	{// 麻将和填大坑俱乐部
	WGS_CLUBMEMBER_LOCK
		
		CenterClubMember ccMember;
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pUser->getUInt32Field("platform_id"), &ccMember))
			return false;
		
		return ccMember.curr_score;
	}
	else
	{
		return sGLMgr.GetItemNum(pChr, ItemModel_Coins);
	}
	
	return 0;
}

// 增加玩家俱乐部当前积分
bool CenterClubMemberManager::AddMemberCurrCoins(const uint32 & club_id, CharPtr & pChr, const uint32 & num)
{
	if(!club_id || pChr.isNull() || !num)
		return false;
	
	Log.Debug("CenterClubMemberManager::AddMemberCurrCoins","club_id[%u] char[%u] num[%u] 111", club_id, pChr->getHandle(), num);
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	if(pUser->getUInt32Field("reg_from") == 5045)
	{
		WGS_CLUBMEMBER_LOCK
		
		CenterClubMember ccMember;
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pUser->getUInt32Field("platform_id"), &ccMember))
			return false;
		
		ccMember.curr_score += num;
		return UpdateCenterClubMember(ccMember);
	}
	else
	{
		sGLMgr.addItemNum(pChr, ItemModel_Coins, num);
		return true;
	}
	
	return false;
}

// 减少玩家俱乐部当前积分
bool CenterClubMemberManager::ReduceMemberCurrCoins(const uint32 & club_id, CharPtr & pChr, const uint32 & num)
{
	if(!club_id || pChr.isNull() || !num)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	Log.Debug("CenterClubMemberManager::ReduceMemberCurrCoins","club_id[%u] pid[%u] num[%u]", club_id, pUser->getUInt32Field("platform_id"), num);
	
	if(pUser->getUInt32Field("reg_from") == 5045)
	{
		WGS_CLUBMEMBER_LOCK
		
		CenterClubMember ccMember;
		if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pUser->getUInt32Field("platform_id"), &ccMember))
			return false;
		
		Log.Debug("CenterClubMemberManager::ReduceMemberCurrCoins","club_id[%u] pid[%u] curr_score[%u] 111", club_id, pUser->getUInt32Field("platform_id"), ccMember.curr_score);
		ccMember.curr_score = ccMember.curr_score > num ? ccMember.curr_score - num : 0;
		
		Log.Debug("CenterClubMemberManager::ReduceMemberCurrCoins","club_id[%u] pid[%u] curr_score[%u] 222", club_id, pUser->getUInt32Field("platform_id"), ccMember.curr_score);
		return UpdateCenterClubMember(ccMember);
	}
	else
	{
		if(sGLMgr.GetItemNum(pChr, ItemModel_Coins) < num)
			return false;
		
		return sGLMgr.reduceItemNum(pChr, ItemModel_Coins, num);
	}
	
	return 0;
}

// 设定房间输赢数据
void CenterClubMemberManager::SetClubRoomWinlose(const uint32 & club_id, const uint32 & club_room_id, const uint32 & pid, const int32 & winLose)
{
	if(!club_id || !club_room_id || !pid || !winLose)
		return ;
	
	CenterClubMember ccMember;
	if(!GetCenterClubMember(club_id, pid, &ccMember))
		return ;
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(ccMember.pump_info, val);
	
	String strRoomId = StringConverter::toString(club_room_id);
	if(val.isMember(strRoomId))
	{// 更新数据
		val[strRoomId] = val[strRoomId].asInt() + winLose;
	}
	else
	{// 设置数据
		val[strRoomId] = winLose;
	}
	
	Json::FastWriter w;
	ccMember.pump_info = w.write(val);
	
	Log.Debug("CenterClubMemberManager::SetClubRoomWinlose","club_id[%u] club_room_id[%u] pid[%u] winLose[%d] pump_info[%s]", club_id, club_room_id, pid, winLose, ccMember.pump_info.c_str());
	UpdateCenterClubMember(ccMember);
}

// 计算房间抽水
int32 CenterClubMemberManager::GetClubRoomWinlose(const uint32 & club_id, const uint32 & club_room_id, const uint32 & pid)
{
	if(!club_id || !club_room_id || !pid)
		return 0;
	
	CenterClubMember ccMember;
	if(!GetCenterClubMember(club_id, pid, &ccMember))
		return 0;
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(ccMember.pump_info, val);
	
	String strRoomId = StringConverter::toString(club_room_id);
	if(val.isMember(strRoomId))
	{// 更新数据
		return val[strRoomId].asInt();
	}
	
	return 0;
}

// 清除房间抽水数据
void CenterClubMemberManager::ClearClubRoomWinlose(const uint32 & club_id, const uint32 & club_room_id, const uint32 & pid)
{
	if(!club_id || !club_room_id || !pid)
		return ;
	
	CenterClubMember ccMember;
	if(!GetCenterClubMember(club_id, pid, &ccMember))
		return ;
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(ccMember.pump_info, val);
	
	String strRoomId = StringConverter::toString(club_room_id);
	if(val.isMember(strRoomId))
		val.removeMember(strRoomId);
	else
		return ;
	
	Json::FastWriter w;
	ccMember.pump_info = w.write(val);
	
	UpdateCenterClubMember(ccMember);
}

//===========================俱乐部成员相关==================================

//===========================俱乐部消息相关==================================
//--------------------------------------------------------
bool CenterClubMessageManager::AddCenterClubMessage(CenterClubMessage & ccMessage)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, &fields);
	
	// uint32 message_id;								// 消息ID
	fields["status"] = ccMessage.status;				// 状态
	fields["agent_id"] = ccMessage.agent_id;			// 代理ID
	fields["club_id"] = ccMessage.club_id;				// 俱乐部ID
	fields["type"] = ccMessage.type;					// 俱乐部ID
	
	fields["owner_cid"] = ccMessage.owner_cid;			// 源角色ID
	fields["owner_pid"] = ccMessage.owner_pid;			// 源PID
	fields["owner_nick"] = ccMessage.owner_nick;		// 源昵称
	fields["owner_icon"] = ccMessage.owner_icon;		// 源头像
	
	fields["target_pid"] = ccMessage.target_pid;		// 目标PID
	fields["target_nick"] = ccMessage.target_nick;		// 目标昵称
	fields["target_icon"] = ccMessage.target_icon;		// 目标头像
	fields["takein_coins"] = ccMessage.takein_coins;	// 带入
	
	fields["message"] = ccMessage.message;				// 消息内容
	fields["create_time"] = ccMessage.create_time;		// 创建时间
	fields["finish_time"] = ccMessage.finish_time;		// 结束时间
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, &fields))
		return false;
	
	ccMessage.message_id = fields["message_id"].GetUInt64();
	return true;
}

//--------------------------------------------------------
bool CenterClubMessageManager::UpdateCenterClubMessage(const CenterClubMessage & ccMessage)
{
	Fields fields;
	
	fields["status"] = ccMessage.status;
	fields["agent_id"] = ccMessage.agent_id;
	fields["club_id"] = ccMessage.club_id;
	fields["type"] = ccMessage.type;
	
	fields["owner_cid"] = ccMessage.owner_cid;
	fields["owner_pid"] = ccMessage.owner_pid;
	fields["owner_nick"] = ccMessage.owner_nick;
	fields["owner_icon"] = ccMessage.owner_icon;
	
	fields["target_pid"] = ccMessage.target_pid;
	fields["target_nick"] = ccMessage.target_nick;
	fields["target_icon"] = ccMessage.target_icon;
	fields["takein_coins"] = ccMessage.takein_coins;
	
	fields["message"] = ccMessage.message;
	fields["create_time"] = ccMessage.create_time;
	fields["finish_time"] = ccMessage.finish_time;
	
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, &fields, "`message_id` = %u", ccMessage.message_id);
	return count > 0;
}

//--------------------------------------------------------
bool CenterClubMessageManager::UpdateCenterClubMessage(Fields * fields, const char * QueryString, ...)
{
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, QueryString); 
	vsnprintf(sql, SQLBUFER_LEN, QueryString, vlist); 
	va_end(vlist);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, fields, sql);
	return count > 0;	
}

//--------------------------------------------------------
bool CenterClubMessageManager::UpdateCenterClubMessage(const uint64 &message_id, Fields * fields)
{
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, fields, "`message_id` = %u", message_id);
	return count > 0;	
}

//--------------------------------------------------------
bool CenterClubMessageManager::GetCenterClubMessage(const uint64 &message_id, CenterClubMessage * ccMessage)
{
	assert (ccMessage);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, &rows, "status < 255 and `message_id` = %u", message_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	ccMessage->message_id = fields["message_id"].GetUInt64();
	ccMessage->status = fields["status"].GetUInt32();
	ccMessage->agent_id = fields["agent_id"].GetUInt32();
	ccMessage->club_id = fields["club_id"].GetUInt64();
	ccMessage->type = fields["type"].GetUInt32();
	
	ccMessage->owner_cid = fields["owner_cid"].GetUInt32();
	ccMessage->owner_pid = fields["owner_pid"].GetUInt32();
	ccMessage->owner_nick = fields["owner_nick"].GetValue();
	ccMessage->owner_icon = fields["owner_icon"].GetValue();
	
	ccMessage->target_pid = fields["target_pid"].GetUInt32();
	ccMessage->target_nick = fields["target_nick"].GetValue();
	ccMessage->target_icon = fields["target_icon"].GetValue();
	ccMessage->takein_coins = fields["takein_coins"].GetUInt32();
	
	ccMessage->message = fields["message"].GetValue();
	ccMessage->create_time = fields["create_time"].GetValue();
	ccMessage->finish_time = fields["finish_time"].GetValue();
	return true;
}

//--------------------------------------------------------
uint32 CenterClubMessageManager::GetCenterClubMessageList(std::list<CenterClubMessage> * lstClub, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstClub->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBMESSAGE, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubMessage ccMessage;
		Fields &fields = *iter;
		
		ccMessage.message_id = fields["message_id"].GetUInt64();
		ccMessage.status = fields["status"].GetUInt32();
		ccMessage.agent_id = fields["agent_id"].GetUInt32();
		ccMessage.club_id = fields["club_id"].GetUInt32();
		ccMessage.type = fields["type"].GetUInt32();
		
		ccMessage.owner_cid = fields["owner_cid"].GetUInt32();
		ccMessage.owner_pid = fields["owner_pid"].GetUInt32();
		ccMessage.owner_nick = fields["owner_nick"].GetValue();
		ccMessage.owner_icon = fields["owner_icon"].GetValue();
		
		ccMessage.target_pid = fields["target_pid"].GetUInt32();
		ccMessage.target_nick = fields["target_nick"].GetValue();
		ccMessage.target_icon = fields["target_icon"].GetValue();
		ccMessage.takein_coins = fields["takein_coins"].GetUInt32();
		
		ccMessage.message = fields["message"].GetValue();
		ccMessage.create_time = fields["create_time"].GetValue();
		ccMessage.finish_time = fields["finish_time"].GetValue();
		
		lstClub->push_back(ccMessage);
	}
	
	return lstClub->size();
}

// 获得某俱乐部最近N条聊天消息（默认取10条）
void CenterClubMessageManager::GetClubNearestMessages(const uint64 & club_id, std::list<CenterClubMessage> * lstClubMessage, const uint32 & limit_count)
{
	if(club_id == 0 || NULL == lstClubMessage || limit_count == 0)
		return ;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return ;
	
	std::list<CenterClubMessage> lstData;
	sCenterClubMessageMgr.GetCenterClubMessageList(&lstData, "status < 255 and club_id = %u and type = %u order by create_time desc limit %u", 
						club_id, enClubMessageType_Chat, limit_count);
	
	std::list<CenterClubMessage>::const_reverse_iterator iter = lstData.rbegin(), iterEnd = lstData.rend();
	for(; iter!=iterEnd; ++iter)
	{
		lstClubMessage->push_back(*iter);
	}
}

// 俱乐部审核消息列表
void CenterClubMessageManager::GetClubReviewMessages(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull() || !club_id)
		return ;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))	// 公会不存在
		return ;
	
	std::list<CenterClubMessage> lstClubMessage;
	sCenterClubMessageMgr.GetCenterClubMessageList(&lstClubMessage, "status < 255 and club_id = %u and type = %u order by create_time desc", 
												club_id, enClubMessageType_Review);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubJoinMessageListPacket(&packet, club_id, lstClubMessage))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

// 俱乐部审核消息列表数量
void CenterClubMessageManager::GetClubReviewMessagesCount(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull() || !club_id)
		return ;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))	// 公会不存在
		return ;
	
	std::list<CenterClubMessage> lstClubMessage;
	sCenterClubMessageMgr.GetCenterClubMessageList(&lstClubMessage, "status < 255 and club_id = %u and type = %u order by create_time desc", 
												club_id, enClubMessageType_Review);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, club_id, enClubMessageType_Review, lstClubMessage.size(), cc.name, true))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

// 俱乐部带入消息列表
void CenterClubMessageManager::GetClubTakeinMessages(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull() || !club_id)
		return ;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))	// 公会不存在
		return ;
	
	std::list<CenterClubMessage> lstClubMessage;
	sCenterClubMessageMgr.GetCenterClubMessageList(&lstClubMessage, "status < 255 and club_id = %u and type = %u order by create_time desc", 
												club_id, enClubMessageType_Takein);
	
	WorldPacket packet;	
	if(sProtocolsMgr.CreateClubTakeinMessageListPacket(&packet, club_id, lstClubMessage))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

// 俱乐部带入消息列表数量
void CenterClubMessageManager::GetClubTakeinMessagesCount(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull() || !club_id)
		return ;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))	// 公会不存在
		return ;
	
	std::list<CenterClubMessage> lstClubMessage;
	sCenterClubMessageMgr.GetCenterClubMessageList(&lstClubMessage, "status < 255 and club_id = %u and type = %u order by create_time desc", 
												club_id, enClubMessageType_Takein);
	
	WorldPacket packet;	
	if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, club_id, enClubMessageType_Takein, lstClubMessage.size(), cc.name, true))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

// 添加审核消息
bool CenterClubMessageManager::AddReviewMessage(const uint64 & club_id, UserPtr & pUser, String & content, const uint32 & referee_id, String & strRefereeName)
{
	if(pUser.isNull())
		return false;
	
	CharPtr pChr = sCharMgr.getByHandle(pUser->getUInt32Field("char_id"));
	if(pChr.isNull())
		return false;
	
	CenterClubMessage ccMessage;
	
	ccMessage.status = 1;
	ccMessage.agent_id = pUser->getUInt32Field("reg_from");
	ccMessage.club_id = club_id;
	ccMessage.type = enClubMessageType_Review;
	
	ccMessage.owner_cid = pChr->getHandle();						// 角色ID
	ccMessage.owner_pid = pUser->getUInt32Field("platform_id");		// 源PID
	ccMessage.owner_nick = pChr->getStringField("name");			// 昵称
	ccMessage.owner_icon = pChr->getStringField("desc");			// 头像
	
	ccMessage.target_pid = referee_id;								// 推荐人ID
	ccMessage.target_nick = strRefereeName;							// 推荐人昵称
	
	ccMessage.message = content;									// 消息内容
	ccMessage.create_time = sGLMgr.GetCurDateTime();				// 创建时间
	
	return AddCenterClubMessage(ccMessage);
}

// 添加带入消息
bool CenterClubMessageManager::AddTakeinCoinsMessage(const uint64 & club_id, UserPtr & pUser, const uint32 & coins)
{
	if(pUser.isNull())
		return false;
	
	CharPtr pChr = sCharMgr.getByHandle(pUser->getUInt32Field("char_id"));
	if(pChr.isNull())
		return false;
	
	CenterClubMessage ccMessage;
	
	ccMessage.status = 1;
	ccMessage.agent_id = pUser->getUInt32Field("reg_from");
	ccMessage.club_id = club_id;
	ccMessage.type = enClubMessageType_Takein;
	
	ccMessage.owner_cid = pChr->getHandle();						// 源角色ID
	ccMessage.owner_pid = pUser->getUInt32Field("platform_id");		// 源PID
	ccMessage.owner_nick = pChr->getStringField("name");			// 昵称
	ccMessage.owner_icon = pChr->getStringField("desc");			// 头像
	
	ccMessage.takein_coins = coins;									// 带入金额
	ccMessage.create_time = sGLMgr.GetCurDateTime();				// 创建时间
	
	return AddCenterClubMessage(ccMessage);
}

// 添加聊天消息（玩家/系统）
uint64 CenterClubMessageManager::AddChatMessage(CharPtr & pChr, String & content, const uint64 & club_id)
{
	if(pChr.isNull())
		return 0;
	
	UserPtr pUser = sUserMgr.load(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return 0;
	
	CenterClubMessage ccMessage;
	
	ccMessage.status = 1;
	ccMessage.agent_id = pUser->getUInt32Field("reg_from");
	ccMessage.club_id = club_id;
	ccMessage.type = enClubMessageType_Chat;
	
	ccMessage.owner_cid = pChr->getHandle();						// 源角色ID
	ccMessage.owner_pid = pUser->getUInt32Field("platform_id");		// 源PID
	ccMessage.owner_nick = pChr->getStringField("name");			// 昵称
	ccMessage.owner_icon = pChr->getStringField("desc");			// 头像
	
	ccMessage.message = content;									// 消息
	ccMessage.create_time = sGLMgr.GetCurDateTime();				// 创建时间
	
	AddCenterClubMessage(ccMessage);
	
	return ccMessage.message_id;
}

bool CenterClubMessageManager::AddSystemChatMessage(String & content, const uint32 & agent_id, const uint64 & club_id)
{
	CenterClubMessage ccMessage;
	
	ccMessage.status = 1;
	ccMessage.agent_id = agent_id;
	ccMessage.club_id = club_id;
	ccMessage.type = enClubMessageType_Chat;
	
	ccMessage.owner_nick = "系统消息";								// 可以不发给客户端，让客户端区分颜色
	ccMessage.message = content;									// 消息
	ccMessage.create_time = sGLMgr.GetCurDateTime();				// 创建时间
	
	return AddCenterClubMessage(ccMessage);
}

// 请求聊天信息列表
bool CenterClubMessageManager::GetClubChatMessageList(CharPtr & pChr, const uint64 & club_id, const uint32 & limit_count)
{
	if(pChr.isNull() || club_id == 0 || limit_count == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return false;
	
	std::list<CenterClubMessage> lstClubMessage;
	GetCenterClubMessageList(&lstClubMessage, "status < 255 and club_id = %u and type = %u order by create_time desc limit %u", 
													club_id, enClubMessageType_Chat, limit_count);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubChatlistPacket(&packet, club_id, lstClubMessage))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

//===========================俱乐部消息相关==================================


//===========================俱乐部配置相关==================================
// 获得俱乐部配置列表
uint32 CenterClubConfigManager::GetCenterClubConfigList(std::list<CenterClubConfig> * lstConfig, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstConfig->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBCONFIG, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubConfig ccConfig;
		Fields &fields = *iter;
		
		ccConfig.config_id = fields["config_id"].GetUInt32();
		ccConfig.status = fields["status"].GetUInt32();
		ccConfig.type = fields["type"].GetUInt32();
		ccConfig.agent_id = fields["agent_id"].GetUInt32();
		ccConfig.rank = fields["rank"].GetUInt32();
		ccConfig.pay_diamand = fields["pay_diamand"].GetUInt32();
		// ccConfig.need_diamand = fields["need_diamand"].GetUInt32();
		ccConfig.valid_days = fields["valid_days"].GetUInt32();
		ccConfig.max_club_add = fields["max_club_add"].GetUInt32();
		ccConfig.max_member_add = fields["max_member_add"].GetUInt32();
		ccConfig.give_coins = fields["give_coins"].GetUInt32();
		ccConfig.max_room_add = fields["max_room_add"].GetUInt32();
		ccConfig.vice_president_add = fields["vice_president_add"].GetUInt32();
		ccConfig.max_join_club = fields["max_join_club"].GetUInt32();
		
		lstConfig->push_back(ccConfig);
	}
	
	return lstConfig->size();
}

// 获得单条配置记录
CenterClubConfig CenterClubConfigManager::GetCenterClubConfig(const uint32 & type, const uint32 & rank, const uint32 & agent_id)
{
	CenterClubConfig config;
	
	std::list<CenterClubConfig> lstConfig;
	GetCenterClubConfigList(&lstConfig, "status < 255 and type = %u and rank = %u and agent_id = %u", type, rank, agent_id);
	
	if(lstConfig.size())
		config = *lstConfig.begin();
	
	return config;
}

// 获得商品配置信息
CenterClubConfig CenterClubConfigManager::GetCenterClubGoodsConfig(const uint32 & goods_id, const uint32 & agent_id)
{
	CenterClubConfig config;
	
	std::list<CenterClubConfig> lstConfig;
	GetCenterClubConfigList(&lstConfig, "status < 255 and type = %u and agent_id = %u and config_id = %u limit 1", enCenterClubConfigType_Shop, agent_id, goods_id);

	if(lstConfig.size())
		config = *lstConfig.begin();
	
	return config;
}

//===========================俱乐部配置相关==================================


//===========================俱乐部日志相关==================================
bool CenterClubLogsManager::AddCenterClubLog(CenterClubLog & ccLog)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, &fields);
	
	// uint64 log_id;									// 消息ID
	fields["status"] = ccLog.status;				// 状态
	fields["status_desc"] = ccLog.status_desc;		// 状态描述
	
	fields["type"] = ccLog.type;					// 类型
	fields["agent_id"] = ccLog.agent_id;			// 代理商ID
	fields["club_id"] = ccLog.club_id;				// 俱乐部ID
	
	fields["pid"] = ccLog.pid;						// 玩家PID
	fields["nick"] = ccLog.nick;					// 玩家昵称
	fields["icon"] = ccLog.icon;					// 玩家头像
	
	fields["data1"] = ccLog.data1;
	fields["data2"] = ccLog.data2;
	fields["data3"] = ccLog.data3;
	fields["data4"] = ccLog.data4;
	fields["data5"] = ccLog.data5;
	fields["data6"] = ccLog.data6;
	fields["data7"] = ccLog.data7;
	fields["data8"] = ccLog.data8;
	
	fields["udata1"] = ccLog.udata1;
	fields["udata2"] = ccLog.udata2;
	fields["udata3"] = ccLog.udata3;
	fields["udata4"] = ccLog.udata4;
	
	fields["fdata1"] = ccLog.fdata1;
	fields["fdata2"] = ccLog.fdata2;
	fields["fdata3"] = ccLog.fdata3;
	fields["fdata4"] = ccLog.fdata4;
	fields["create_time"] = ccLog.create_time;
	
	Log.Debug("CenterClubLogsManager::AddCenterClubLog","create_time[%s]", ccLog.create_time.c_str());
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, &fields))
		return false;
	
	ccLog.log_id = fields["log_id"].GetUInt64();
	return true;
}

bool CenterClubLogsManager::UpdateCenterClubLog(const CenterClubLog & ccLog)
{
	Fields fields;
	
	fields["status"] = ccLog.status;				// 状态
	fields["status_desc"] = ccLog.status_desc;		// 状态描述
	
	fields["type"] = ccLog.type;					// 类型
	fields["agent_id"] = ccLog.agent_id;			// 代理商ID
	fields["club_id"] = ccLog.club_id;				// 俱乐部ID
	
	fields["pid"] = ccLog.pid;						// 玩家PID
	fields["nick"] = ccLog.nick;					// 玩家昵称
	fields["icon"] = ccLog.icon;					// 玩家头像
	
	fields["data1"] = ccLog.data1;
	fields["data2"] = ccLog.data2;
	fields["data3"] = ccLog.data3;
	fields["data4"] = ccLog.data4;
	fields["data5"] = ccLog.data5;
	fields["data6"] = ccLog.data6;
	fields["data7"] = ccLog.data7;
	fields["data8"] = ccLog.data8;
	
	fields["udata1"] = ccLog.udata1;
	fields["udata2"] = ccLog.udata2;
	fields["udata3"] = ccLog.udata3;
	fields["udata4"] = ccLog.udata4;
	
	fields["fdata1"] = ccLog.fdata1;
	fields["fdata2"] = ccLog.fdata2;
	fields["fdata3"] = ccLog.fdata3;
	fields["fdata4"] = ccLog.fdata4;
	fields["create_time"] = ccLog.create_time;
	
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, &fields, "`log_id` = %u", ccLog.log_id);
	return count > 0;
}

bool CenterClubLogsManager::UpdateCenterClubLog(Fields * fields, const char * QueryString, ...)
{
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, QueryString); 
	vsnprintf(sql, SQLBUFER_LEN, QueryString, vlist); 
	va_end(vlist);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, fields, sql);
	return count > 0;	
}

bool CenterClubLogsManager::UpdateCenterClubLog(const uint64 & log_id, Fields * fields)
{
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, fields, "`log_id` = %u", log_id);
	return count > 0;
}

// 获得俱乐部日志
bool CenterClubLogsManager::GetCenterClubLog(const uint64 & ccLog_id, CenterClubLog * ccLog)
{
	assert (ccLog);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, &rows, "`log_id` = %u", ccLog_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	ccLog->log_id = fields["log_id"].GetUInt64();
	ccLog->status = fields["status"].GetUInt32();
	ccLog->status_desc = fields["status_desc"].GetValue();
	
	ccLog->type = fields["type"].GetUInt32();
	ccLog->agent_id = fields["agent_id"].GetUInt32();
	ccLog->club_id = fields["club_id"].GetUInt64();
	
	ccLog->pid = fields["pid"].GetUInt32();
	ccLog->nick = fields["nick"].GetValue();
	ccLog->icon = fields["icon"].GetValue();
	
	ccLog->data1 = fields["data1"].GetInt32();
	ccLog->data2 = fields["data2"].GetInt32();
	ccLog->data3 = fields["data3"].GetInt32();
	ccLog->data4 = fields["data4"].GetInt32();
	ccLog->data5 = fields["data5"].GetInt32();
	ccLog->data6 = fields["data6"].GetInt32();
	ccLog->data7 = fields["data7"].GetInt32();
	ccLog->data8 = fields["data8"].GetInt32();
	
	ccLog->udata1 = fields["udata1"].GetInt32();
	ccLog->udata2 = fields["udata2"].GetInt32();
	ccLog->udata3 = fields["udata3"].GetInt32();
	ccLog->udata4 = fields["udata4"].GetInt32();
	
	ccLog->fdata1 = fields["fdata1"].GetFloat();
	ccLog->fdata2 = fields["fdata2"].GetFloat();
	ccLog->fdata3 = fields["fdata3"].GetFloat();
	ccLog->fdata4 = fields["fdata4"].GetFloat();
	
	ccLog->create_time = fields["create_time"].GetValue();
	return true;
}

// 获得俱乐部日志列表
uint32 CenterClubLogsManager::GetCenterClubLogList(std::list<CenterClubLog> * lstClubLog, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstClubLog->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	Log.Debug("CenterClubLogsManager::GetCenterClubLogList","db name[%s] sql[%s]", DB_NAME_GAMECLUBLOGS, sql);
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLOGS, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubLog ccLog;
		Fields &fields = *iter;
		
		ccLog.log_id = fields["log_id"].GetUInt64();
		ccLog.status = fields["status"].GetUInt32();
		ccLog.status_desc = fields["status_desc"].GetValue();
		
		ccLog.type = fields["type"].GetUInt32();
		ccLog.agent_id = fields["agent_id"].GetUInt32();
		ccLog.club_id = fields["club_id"].GetUInt64();
		
		ccLog.pid = fields["pid"].GetUInt32();
		ccLog.nick = fields["nick"].GetValue();
		ccLog.icon = fields["icon"].GetValue();
		
		ccLog.data1 = fields["data1"].GetInt32();
		ccLog.data2 = fields["data2"].GetInt32();
		ccLog.data3 = fields["data3"].GetInt32();
		ccLog.data4 = fields["data4"].GetInt32();
		ccLog.data5 = fields["data5"].GetInt32();
		ccLog.data6 = fields["data6"].GetInt32();
		ccLog.data7 = fields["data7"].GetInt32();
		ccLog.data8 = fields["data8"].GetInt32();
		
		ccLog.udata1 = fields["udata1"].GetInt32();
		ccLog.udata2 = fields["udata2"].GetInt32();
		ccLog.udata3 = fields["udata3"].GetInt32();
		ccLog.udata4 = fields["udata4"].GetInt32();
		
		ccLog.fdata1 = fields["fdata1"].GetFloat();
		ccLog.fdata2 = fields["fdata2"].GetFloat();
		ccLog.fdata3 = fields["fdata3"].GetFloat();
		ccLog.fdata4 = fields["fdata4"].GetFloat();
		
		ccLog.create_time = fields["create_time"].GetValue();
		lstClubLog->push_back(ccLog);
	}
	
	return lstClubLog->size();
}

// 增加审核加入日志
bool CenterClubLogsManager::AddReviewLog(CenterClubMessage & ccMessage, const uint8 & result)
{
	CenterClubLog ccLog;
	ccLog.status = result;
		
	ccLog.type = enClubLogType_Review;
	ccLog.agent_id = ccMessage.agent_id;
	ccLog.club_id = ccMessage.club_id;
		
	ccLog.pid = ccMessage.owner_pid;
	ccLog.nick = ccMessage.owner_nick;
	ccLog.icon = ccMessage.owner_icon;
	ccLog.create_time = sGLMgr.GetCurDateTime();
	
	return AddCenterClubLog(ccLog);
}

// 增加带入日志
bool CenterClubLogsManager::AddTakeinLog(CenterClubMessage & ccMessage, const uint8 & result)
{
	CenterClubLog ccLog;
	ccLog.status = result;
	
	ccLog.type = enClubLogType_Takein;
	ccLog.agent_id = ccMessage.agent_id;
	ccLog.club_id = ccMessage.club_id;
	
	ccLog.pid = ccMessage.owner_pid;
	ccLog.nick = ccMessage.owner_nick;
	ccLog.icon = ccMessage.owner_icon;
	ccLog.data1 = ccMessage.takein_coins;
	
	ccLog.create_time = sGLMgr.GetCurDateTime();
	return AddCenterClubLog(ccLog);
}

// 增加退分日志
bool CenterClubLogsManager::AddExitCoinsLog(CenterClubMember & ccMember)
{
	CenterClubLog ccLog;
	ccLog.status = 1;
	
	ccLog.type = enClubLogType_ExitCoins;
	ccLog.agent_id = ccMember.agent_id;
	ccLog.club_id = ccMember.club_id;
	
	ccLog.pid = ccMember.pid;
	ccLog.nick = ccMember.nick;
	ccLog.icon = ccMember.icon_url;
	ccLog.data1 = ccMember.curr_score;		// 成绩输赢
	ccLog.data2 = ccMember.play_loop;		// 游戏局数
	ccLog.data3 = ccMember.takein_coins;	// 带入金额
	ccLog.data4 = ccMember.total_pump;		// 抽水金额
	
	ccLog.create_time = sGLMgr.GetCurDateTime();
	return AddCenterClubLog(ccLog);
}

// 增加活跃变化日志
bool CenterClubLogsManager::AddActivityChangeLog(CenterClub & ccClub, const uint32 & num)
{
	CenterClubLog ccLog;
	ccLog.status = 1;
	
	ccLog.type = enClubLogType_Activity;
	ccLog.agent_id = ccClub.agent_id;
	ccLog.club_id = ccClub.club_id;
	
	ccLog.pid = ccClub.owner_pid;
	ccLog.nick = ccClub.owner_nick;
	
	ccLog.data1 = ccClub.show_id;					// 俱乐部显示ID
	ccLog.data2 = num;								// 活跃增加
	ccLog.data3 = ccClub.activity_value;			// 当前活跃
	
	ccLog.create_time = sGLMgr.GetCurDateTime();
	return AddCenterClubLog(ccLog);
}

// 增加手续费日志
bool CenterClubLogsManager::AddTakeinPumpLog(CenterClubMember & ccMember, const uint32 & num)
{
	CenterClubLog ccLog;
	ccLog.status = 1;
	
	ccLog.type = enClubLogType_TakeinPump;
	ccLog.agent_id = ccMember.agent_id;
	ccLog.club_id = ccMember.club_id;
	
	ccLog.pid = ccMember.pid;
	ccLog.nick = ccMember.nick;
	ccLog.icon = ccMember.icon_url;
	
	CenterClub cc;
	sCenterClubMgr.GetCenterClub(ccMember.club_id, &cc);
	
	ccLog.data1 = cc.show_id;				// 俱乐部显示ID
	ccLog.data2 = num;						// 手续费数量
	
	ccLog.create_time = sGLMgr.GetCurDateTime();
	return AddCenterClubLog(ccLog);
}

// 获取审核历史
bool CenterClubLogsManager::GetReviewLogs(CharPtr & pChr, const uint64 & club_id, const uint8 & type)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	// uint32 m_agent_id = pUser->getUInt32Field("reg_from");
	
	// 公会不存在
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return false;
	
	// 不是公会成员
	CenterClubMember ccMember;
	if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		return false;
	
	// 普通成员不能查看
	if(ccMember.type == enClubMemberType_Normal)
		return false;
	
	std::list<CenterClubLog> lstClubLog;
	GetCenterClubLogList(&lstClubLog, "status < 255 and club_id = %u and type = %u order by create_time desc", club_id, type);
	
	Log.Debug("CenterClubLogsManager::GetReviewLogs","club_id[%u] type[%u] lstClubLog[%u]", club_id, type, lstClubLog.size());
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubReviewLogsPacket(&packet, club_id, type, lstClubLog))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 获取退分历史
bool CenterClubLogsManager::GetExitCoinsLogs(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	// uint32 m_agent_id = pUser->getUInt32Field("reg_from");
	
	// 公会不存在
	CenterClub cc;
	if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		return false;
	
	// 不是公会成员
	CenterClubMember ccMember;
	if(!sCenterClubMemberMgr.GetCenterClubMember(club_id, pid, &ccMember))
		return false;
	
	// 普通成员不能查看
	if(ccMember.type == enClubMemberType_Normal)
		return false;
	
	std::list<CenterClubLog> lstClubLog;
	GetCenterClubLogList(&lstClubLog, "status < 255 and club_id = %u and type = %u order by create_time desc", club_id, enClubLogType_ExitCoins);
	
	Log.Debug("CenterClubLogsManager::GetReviewLogs","club_id[%u] type[%u] lstClubLog[%u]", club_id, enClubLogType_ExitCoins, lstClubLog.size());
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubExitCoinsLogsPacket(&packet, club_id, lstClubLog))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

//===========================俱乐部日志相关==================================


//==============================俱乐部房间相关===============================
CenterClubRoomManager::CenterClubRoomManager()
{
	m_nLastUpdateTimer = time(0);
	Log.Debug("CenterClubRoomManager::CenterClubRoomManager","m_nLastUpdateTimer[%u]", m_nLastUpdateTimer);
}

// 俱乐部房间
bool CenterClubRoomManager::AddCenterClubRoom(CenterClubRoom & ccRoom)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &fields);
	
	// uint64 room_id;
	fields["status"] = ccRoom.status;
	fields["type"] = ccRoom.type;
	
	fields["club_id"] = ccRoom.club_id;
	fields["agent_id"] = ccRoom.agent_id;
	
	fields["pid"] = ccRoom.pid;
	fields["curr_member"] = ccRoom.curr_member;
	fields["max_member"] = ccRoom.max_member;
	
	fields["play_mode"] = ccRoom.play_mode;
	fields["expiry_date"] = ccRoom.expiry_date;
	
	fields["data1"] = ccRoom.data1;
	fields["data2"] = ccRoom.data2;
	fields["data3"] = ccRoom.data3;
	fields["data4"] = ccRoom.data4;
	fields["data5"] = ccRoom.data5;
	fields["data6"] = ccRoom.data6;
	fields["data7"] = ccRoom.data7;
	fields["data8"] = ccRoom.data8;
	
	fields["sdata1"] = ccRoom.sdata1;
	fields["sdata2"] = ccRoom.sdata2;
	fields["sdata3"] = ccRoom.sdata3;
	fields["sdata4"] = ccRoom.sdata4;
	fields["sdata5"] = ccRoom.sdata5;
	fields["sdata6"] = ccRoom.sdata6;
	fields["sdata7"] = ccRoom.sdata7;
	fields["sdata8"] = ccRoom.sdata8;
	
	fields["create_time"] = ccRoom.create_time;
	fields["end_time"] = ccRoom.end_time;
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &fields))
		return false;
	
	ccRoom.room_id = fields["room_id"].GetUInt64();
	return true;
}

bool CenterClubRoomManager::UpdateCenterClubRoom(const CenterClubRoom & ccRoom)
{
	Fields fields;
	fields["status"] = ccRoom.status;
	fields["type"] = ccRoom.type;
	
	fields["club_id"] = ccRoom.club_id;
	fields["agent_id"] = ccRoom.agent_id;
	
	fields["pid"] = ccRoom.pid;
	fields["curr_member"] = ccRoom.curr_member;
	fields["max_member"] = ccRoom.max_member;
	
	fields["play_mode"] = ccRoom.play_mode;
	fields["expiry_date"] = ccRoom.expiry_date;
	
	fields["data1"] = ccRoom.data1;
	fields["data2"] = ccRoom.data2;
	fields["data3"] = ccRoom.data3;
	fields["data4"] = ccRoom.data4;
	fields["data5"] = ccRoom.data5;
	fields["data6"] = ccRoom.data6;
	fields["data7"] = ccRoom.data7;
	fields["data8"] = ccRoom.data8;
	
	fields["sdata1"] = ccRoom.sdata1;
	fields["sdata2"] = ccRoom.sdata2;
	fields["sdata3"] = ccRoom.sdata3;
	fields["sdata4"] = ccRoom.sdata4;
	fields["sdata5"] = ccRoom.sdata5;
	fields["sdata6"] = ccRoom.sdata6;
	fields["sdata7"] = ccRoom.sdata7;
	fields["sdata8"] = ccRoom.sdata8;
	
	fields["end_time"] = ccRoom.end_time;
	
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &fields, "`room_id` = %u", ccRoom.room_id);
	return count > 0;
}

bool CenterClubRoomManager::GetCenterClubRoom(const uint64 & room_id, CenterClubRoom * ccRoom)
{
	assert (ccRoom);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &rows, "`room_id` = %u", room_id);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	ccRoom->room_id	= fields["room_id"].GetUInt64();
	ccRoom->status = fields["status"].GetUInt32();
	ccRoom->type = fields["type"].GetUInt32();
	
	ccRoom->club_id = fields["club_id"].GetUInt64();
	ccRoom->agent_id = fields["agent_id"].GetUInt32();
	ccRoom->pid = fields["pid"].GetUInt32();
	
	ccRoom->curr_member = fields["curr_member"].GetUInt32();
	ccRoom->max_member = fields["max_member"].GetUInt32();
	
	ccRoom->play_mode = fields["play_mode"].GetValue();
	ccRoom->expiry_date = fields["expiry_date"].GetValue();
	
	ccRoom->data1 = fields["data1"].GetInt32();
	ccRoom->data2 = fields["data2"].GetInt32();
	ccRoom->data3 = fields["data3"].GetInt32();
	ccRoom->data4 = fields["data4"].GetInt32();
	ccRoom->data5 = fields["data5"].GetInt32();
	ccRoom->data6 = fields["data6"].GetInt32();
	ccRoom->data7 = fields["data7"].GetInt32();
	ccRoom->data8 = fields["data8"].GetInt32();
	
	ccRoom->sdata1 = fields["sdata1"].GetValue();
	ccRoom->sdata2 = fields["sdata2"].GetValue();
	ccRoom->sdata3 = fields["sdata3"].GetValue();
	ccRoom->sdata4 = fields["sdata4"].GetValue();
	ccRoom->sdata5 = fields["sdata5"].GetValue();
	ccRoom->sdata6 = fields["sdata6"].GetValue();
	ccRoom->sdata7 = fields["sdata7"].GetValue();
	ccRoom->sdata8 = fields["sdata8"].GetValue();
	
	ccRoom->create_time = fields["create_time"].GetValue();
	ccRoom->end_time = fields["end_time"].GetValue();
	return true;
}

uint32 CenterClubRoomManager::GetCenterClubRoomList(std::list<CenterClubRoom> * lstClubRoom, const char * query,...)
{
	va_list argp;
	va_start(argp, query);
	lstClubRoom->clear();
	FieldRows fieldRows;
	
	char sql[SQLBUFER_LEN] = ""; 
	va_list vlist; 
	va_start(vlist, query); 
	vsnprintf(sql, SQLBUFER_LEN, query, vlist); 
	va_end(vlist);
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &fieldRows, sql))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubRoom ccRoom;
		Fields &fields = *iter;
		
		ccRoom.room_id	= fields["room_id"].GetUInt64();
		ccRoom.status = fields["status"].GetUInt32();
		ccRoom.type = fields["type"].GetUInt32();
		
		ccRoom.club_id = fields["club_id"].GetUInt64();
		ccRoom.agent_id = fields["agent_id"].GetUInt32();
		ccRoom.pid = fields["pid"].GetUInt32();
		
		ccRoom.curr_member = fields["curr_member"].GetUInt32();
		ccRoom.max_member = fields["max_member"].GetUInt32();
		ccRoom.play_mode = fields["play_mode"].GetValue();
		ccRoom.expiry_date = fields["expiry_date"].GetValue();
		
		ccRoom.data1 = fields["data1"].GetInt32();
		ccRoom.data2 = fields["data2"].GetInt32();
		ccRoom.data3 = fields["data3"].GetInt32();
		ccRoom.data4 = fields["data4"].GetInt32();
		ccRoom.data5 = fields["data5"].GetInt32();
		ccRoom.data6 = fields["data6"].GetInt32();
		ccRoom.data7 = fields["data7"].GetInt32();
		ccRoom.data8 = fields["data8"].GetInt32();
		
		ccRoom.sdata1 = fields["sdata1"].GetValue();
		ccRoom.sdata2 = fields["sdata2"].GetValue();
		ccRoom.sdata3 = fields["sdata3"].GetValue();
		ccRoom.sdata4 = fields["sdata4"].GetValue();
		ccRoom.sdata5 = fields["sdata5"].GetValue();
		ccRoom.sdata6 = fields["sdata6"].GetValue();
		ccRoom.sdata7 = fields["sdata7"].GetValue();
		ccRoom.sdata8 = fields["sdata8"].GetValue();
		
		ccRoom.create_time = fields["create_time"].GetValue();
		ccRoom.end_time = fields["end_time"].GetValue();
		
		lstClubRoom->push_back(ccRoom);
	}
	
	return lstClubRoom->size();
}

uint32 CenterClubRoomManager::GetCenterClubRoomList(std::list<CenterClubRoom> * lstClubRoom, const uint64 & club_id, const uint32 & type)
{
	lstClubRoom->clear();
	FieldRows fieldRows;
	
	if(type > 0)
	{
		if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &fieldRows, "club_id = %u and status < 255 and type = %u", club_id, type))
			return 0;
	}
	else
	{
		if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBROOM, &fieldRows, "club_id = %u and status < 255", club_id))
			return 0;
	}
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubRoom ccRoom;
		Fields &fields = *iter;
		
		ccRoom.room_id	= fields["room_id"].GetUInt64();
		ccRoom.status = fields["status"].GetUInt32();
		ccRoom.type = fields["type"].GetUInt32();
		
		ccRoom.club_id = fields["club_id"].GetUInt64();
		ccRoom.agent_id = fields["agent_id"].GetUInt32();
		ccRoom.pid = fields["pid"].GetUInt32();
		
		ccRoom.curr_member = fields["curr_member"].GetUInt32();
		ccRoom.max_member = fields["max_member"].GetUInt32();
		ccRoom.play_mode = fields["play_mode"].GetValue();
		ccRoom.expiry_date = fields["expiry_date"].GetValue();
		
		ccRoom.data1 = fields["data1"].GetInt32();
		ccRoom.data2 = fields["data2"].GetInt32();
		ccRoom.data3 = fields["data3"].GetInt32();
		ccRoom.data4 = fields["data4"].GetInt32();
		ccRoom.data5 = fields["data5"].GetInt32();
		ccRoom.data6 = fields["data6"].GetInt32();
		ccRoom.data7 = fields["data7"].GetInt32();
		ccRoom.data8 = fields["data8"].GetInt32();
		
		ccRoom.sdata1 = fields["sdata1"].GetValue();
		ccRoom.sdata2 = fields["sdata2"].GetValue();
		ccRoom.sdata3 = fields["sdata3"].GetValue();
		ccRoom.sdata4 = fields["sdata4"].GetValue();
		ccRoom.sdata5 = fields["sdata5"].GetValue();
		ccRoom.sdata6 = fields["sdata6"].GetValue();
		ccRoom.sdata7 = fields["sdata7"].GetValue();
		ccRoom.sdata8 = fields["sdata8"].GetValue();
		
		ccRoom.create_time = fields["create_time"].GetValue();
		ccRoom.end_time = fields["end_time"].GetValue();
		
		lstClubRoom->push_back(ccRoom);
	}
	
	return lstClubRoom->size();
}
	
//=============================游戏逻辑部分================================
// 创建房间
bool CenterClubRoomManager::CreateClubRoom(CharPtr & pChr, const uint64 & club_id, String strData)
{
	if(pChr.isNull() || club_id == 0)
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(strData, val);
	
	uint32 room_id = 0;
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	CenterClub cc;
	
	uint8 result = enCreateClubRoomResult_Sucess;
	do
	{
		if(val.empty())
		{// 参数错误
			result = enCreateClubRoomResult_ErrorParmas;
			break;
		}
		
		if(!sCenterClubMgr.GetCenterClub(club_id, &cc))
		{// 俱乐部信息
			result = enCreateClubRoomResult_ErrorParmas;
			break;
		}
		
		if(!val.isMember("typeWanFa") || !val.isMember("limit_time"))
		{
			result = enCreateClubRoomResult_ErrorParmas;
			break;
		}
		
		if(!sCenterClubMemberMgr.IsPlayerClubManager(club_id, pid))
		{// 不是公会管理员
			result = enCreateClubRoomResult_NotManager;
			break;
		}
		
		if(sCenterClubRoomMgr.IsCreateClubRoomOverLimit(pChr))
		{// 房间数量超过限制
			result = enCreateClubRoomResult_LimitCount;
			break;
		}
		
	}while(0);
	
	if(result == enCreateClubRoomResult_Sucess)
	{// 创建俱乐部房间数据
		
		uint32 expirt_time = time(0) + val["limit_time"].asUInt() * 60;
		// uint32 expirt_time = time(0) + 60;
		
		CenterClubRoom ccRoom;
		ccRoom.status = 1;
		ccRoom.type = val["typeWanFa"].asUInt();					// 类型（1 匹配房 2 选座房）
		ccRoom.club_id = club_id;									// 俱乐部ID
		ccRoom.agent_id = agent_id;									// 代理商ID
		ccRoom.pid = pid;											// 创建玩家PID
		ccRoom.max_member = 200;									// 允许进入的最大玩家数量
		
		ccRoom.play_mode = strData;									// 玩法配置
		ccRoom.expiry_date = sGLMgr.GetCurDateTime(expirt_time);	// 过期时间
		
		Log.Debug("CenterClubRoomManager::CreateClubRoom","char[%u] type[%u] expiry_date[%s] expirt_time[%u]", pChr->getHandle(), ccRoom.type, ccRoom.expiry_date.c_str(), expirt_time);
		ccRoom.create_time = sGLMgr.GetCurDateTime();				// 创建时间
		AddCenterClubRoom(ccRoom);
		room_id = ccRoom.room_id;
		
		val["agent_id"] = agent_id;														// 代理商ID
		val["club_id"] = uint32(club_id);												// 俱乐部ID
		val["club_room_id"] = uint32(room_id);											// 俱乐部房间ID
		val["club_name"] = cc.name;														// 俱乐部名称
		val["club_rank"] = cc.rank;														// 俱乐部星级
		val["start_time"] = uint32(sGLMgr.GetDateTimeSeconds(ccRoom.create_time));		// 房间开始时间
		val["end_time"] = uint32(sGLMgr.GetDateTimeSeconds(ccRoom.expiry_date));		// 房间结束时间
		
		Json::FastWriter w;
		ccRoom.play_mode = w.write(val);
		UpdateCenterClubRoom(ccRoom);
		
		if(ccRoom.type == 2)
		{// 选座房，开线程创建组局
			
			String parmasData = ccRoom.play_mode;
			ThreadPool.ExecuteTask(new CreateClubRoomBattleListThread(parmasData, 50));
		}
	}
	
	// 发送创建结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubCreateRoomResultPacket(&packet, club_id, room_id, strData, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enCreateClubRoomResult_Sucess)
	{// 成功发送俱乐部房间列表
		GetClubRoomList(pChr, club_id);
	}
	return true;
}

// 请求俱乐部房间列表
bool CenterClubRoomManager::GetClubRoomList(CharPtr & pChr, const uint64 & club_id, const uint32 & type)
{
	if(pChr.isNull() || !club_id)
		return false;
	
	std::list<CenterClubRoom> lstClubRoom;
	GetCenterClubRoomList(&lstClubRoom, club_id, type);
	
	// 发送俱乐部房间列表信息
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubRoomListPacket(&packet, club_id, type, lstClubRoom))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	return true;
}

// 房间Update，检查超时房间
void CenterClubRoomManager::Update()
{
	uint32 time_zero = time(0);
	if(m_nLastUpdateTimer && m_nLastUpdateTimer + 60 <= time_zero)
	{
		String end_time = sGLMgr.GetCurDateTime();
		std::list<CenterClubRoom> lstClubRoom;
		GetCenterClubRoomList(&lstClubRoom, "status < 255 and expiry_date < '%s'", end_time.c_str());
		
		if(lstClubRoom.size())
		{// 处理超时俱乐部房间
			
			std::list<CenterClubRoom>::iterator iter, ei = lstClubRoom.end();
			for(iter = lstClubRoom.begin(); iter!=ei ;++iter)
			{
				// 通知所有牌局结束
				std::list<CenterBattle> lstData;
				sCenterBattleMgr.GetCenterBattleList(&lstData, "status < 255 and club_id = %u and club_room_id = %u ", (*iter).club_id, (*iter).room_id);
				
				uint32 index = 0;
				Json::Value val;
				Json::Value valChannel;
				
				uint32 game_id = 0;
				uint32 gz_id = 0;
				if(lstData.size())
				{
					std::list<CenterBattle>::iterator itCB, eiCB = lstData.end();
					for(itCB = lstData.begin(); itCB!=eiCB; ++itCB)
					{// 牌桌ID列表
						valChannel[index++] = (*itCB).channel_id;
						
						game_id = (*itCB).game_id;
						gz_id = (*itCB).gz_id;
					}
					
					// 调用网关 可能每个房间对应的分区不同，所以要一个房间一个房间的调用
					val["channels"] = valChannel;
					val["game_id"] = game_id;
					val["gz_id"] = gz_id;
					val["agent_id"] = (*iter).agent_id;
					
					Json::FastWriter w;
					String strData = w.write(val);
					
					sGLMgr.NoticeClubBattleTimesUp(strData);
				}
				
				ClubRoomComplate(*iter);
			}
			
			Log.Debug("CenterClubRoomManager::Update","club_room over size[%u]", lstClubRoom.size());
		}
		
		m_nLastUpdateTimer = time_zero;
	}
}

// 房间结束
void CenterClubRoomManager::ClubRoomComplate(CenterClubRoom & ccRoom)
{
	// 设置结束时间，修改结束状态
	ccRoom.end_time = sGLMgr.GetCurDateTime();
	ccRoom.status = 255;
	UpdateCenterClubRoom(ccRoom);
	
	// 结算玩家对局费和加到身上的钱
	std::list<CenterBattleLog> logs;
	sCenterBattleLogMgr.GetCenterBattleLogList(&logs, "club_room_id = %u and type = %u", ccRoom.room_id, enCenterBattleLogType_ClubRoomLog);
	
	if(logs.empty())
		return ;
	
	stIncomeInfo stICInfo;
	
	stICInfo.cb_id = ccRoom.room_id;
	stICInfo.channel_id = ccRoom.room_id;
	stICInfo.end_time = sGLMgr.GetCurDateTime();
	stICInfo.agent_id = ccRoom.agent_id;
	
	CenterBattleLog btLog = *logs.begin();
	
	Json::Reader reader;
	Json::Value val;
	reader.parse(btLog.sdata5, val);
	
	uint32 total_sys_pump = 0;		// 系统总抽水（代理分成）
	for (Json::Value::iterator iterVal = val.begin(); iterVal != val.end(); ++iterVal)
	{
		uint32 pid = (*iterVal)["pid"].asUInt();					// PID
		
		CenterClubMember ccMember;
		if(!sCenterClubMemberMgr.GetCenterClubMember(btLog.club_id, pid, &ccMember))
			continue;
		
		// 房间输赢
		int32 roomWinLose = sCenterClubMemberMgr.GetClubRoomWinlose(ccRoom.club_id, ccRoom.room_id, ccMember.pid);
		Log.Debug("CenterClubRoomManager::ClubRoomComplate","club_id[%u] room_id[%u] pid[%u] roomWinLose[%d]", ccRoom.club_id, ccRoom.room_id, ccMember.pid, roomWinLose);
		// 房间抽水（有赢足够多才会有值）
		uint32 room_pump = roomWinLose > 0 ? uint32(roomWinLose * 0.05f) : 0;
		// 系统抽水
		uint32 sys_pump = room_pump;
		
		CharPtr pMemberChar = sGLMgr.GetCharptrByPidAndGzid(ccMember.pid, ccMember.gz_id);
		if(!pMemberChar.isNull())
		{
			AICreatureBaseInterface * pChrBaseAI = TO_CREATURE_BASE_INTERFACE(pMemberChar->getAIInterface());
			if(pChrBaseAI)
			{
				uint32 refer_pid = pChrBaseAI->GetXMLData("REFEE_PID");
				if(room_pump >= 10 && refer_pid)
				{// 给介绍人分红
					
					uint32 gz_id = ccMember.gz_id;
					CharPtr pReferChar = sGLMgr.GetCharptrByPidAndGzid(refer_pid, gz_id);
					if(!pReferChar.isNull())
					{
						uint32 pump_coins = uint32(room_pump * 0.1f);
						sys_pump -= pump_coins;
						
						sGLMgr.addItemNum(pReferChar, ItemModel_Coins, pump_coins);
						sLogDataMgr.UpdateItemsInfoLog(pReferChar, en_ST_ClubFenHong, ItemModel_Coins, en_UNT_Add, pump_coins, ccMember.club_id, ccMember.pid);
						
						std::map<uint32, uint32> mapItems;
						const MailModel *pModel = sXmlDataMgr.GetMailModel("CLUB_GAME_FENHONG");
						if(pModel)
						{
							String szTitle = pModel->szTitle;
							String szContent = pModel->szContent;
							// 玩家【%s】为你赢得了%u游戏币的分红金。
							
							char buf[256] = "";
							sprintf(buf, szContent.c_str(),ccMember.nick.c_str(), pump_coins);
							sGLMgr.CreateMail(ccMember.referee_pid, szTitle, buf, 0, 0, &mapItems);				
						}	
					}
				}
			}
		}
		
		// 扣除抽水，产生收益
		ccMember.curr_score = ccMember.curr_score > room_pump ? ccMember.curr_score - room_pump : 0;
		ccMember.total_pump = ccMember.total_pump > room_pump ? ccMember.total_pump - room_pump : 0;
		// 将成绩中的抽水部分扣除（以具体房间来算）
		ccMember.curr_result -= room_pump;
		sCenterClubMemberMgr.UpdateCenterClubMember(ccMember);
		
		if(sys_pump)
		{// 产生收益
			if(stICInfo.gz_id == 0)
			{
				stICInfo.gz_id = ccMember.gz_id;
				stICInfo.game_id = sGLMgr.GetTargetServerGameid(ccMember.gz_id, ccMember.agent_id);
			}
			
			stIncomePlayerInfo playInfo;
			playInfo.pid = ccMember.pid;
			playInfo.model_id = ItemModel_Coins;
			playInfo.num = sys_pump;
			
			stICInfo.vPlayers.push_back(playInfo);
			
			total_sys_pump += sys_pump;
		}
		
		Log.Debug("CenterClubRoomManager::ClubRoomComplate","club_id[%u] club_room_id[%u] roomWinLose[%d] room_pump[%u] sys_pump[%u]", 
															btLog.club_id, btLog.club_room_id, roomWinLose, room_pump, sys_pump);
	}
	
	if(total_sys_pump)
	{// 增加俱乐部活跃值
		sCenterClubMgr.AddClubActivityValue(btLog.club_id, total_sys_pump);
	}
	
	if(stICInfo.vPlayers.size())
		sGLMgr.AddPlayerIncomeInfoToUrl(stICInfo);
}

// 获取玩家创建的俱乐部房间数量
bool CenterClubRoomManager::IsCreateClubRoomOverLimit(CharPtr & pChr)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	uint32 rank = pChr->getUInt32Field("rank");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	uint32 pid = pUser->getUInt32Field("platform_id");
	
	CenterClubConfig ccConfig = sCenterClubConfigMgr.GetCenterClubConfig(enCenterClubConfigType_Player, rank, agent_id);
	uint32 limit_count = ccConfig.max_room_add;
	
	std::list<CenterClubRoom> lstClubRoom;
	uint32 curr_count = GetCenterClubRoomList(&lstClubRoom, "status < 255 and agent_id = %u and pid = %u", agent_id, pid);
	
	return curr_count >= limit_count;
}

// 俱乐部未结束房间数量
void CenterClubRoomManager::RunningClubRoomCount(CharPtr & pChr, const uint64 & club_id)
{
	if(pChr.isNull() || club_id == 0)
		return ;
	
	std::list<CenterClubRoom> lstClubRoom;
	uint32 room_count = GetCenterClubRoomList(&lstClubRoom, club_id, 0);
	
	String strClubName = "";
	
	WorldPacket packet;
	if(sProtocolsMgr.CreateClubMessageListCountPacket(&packet, club_id, enClubMessageType_RoomCount, room_count, strClubName, true))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
}

//==============================俱乐部房间相关===============================

//==============================俱乐部标签相关===============================
// 添加俱乐部标签
bool CenterClubLabelManager::AddCenterClubLabel(CenterClubLabel & ccLabel)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLABEL, &fields);
	
	// uint32 label_id;
	fields["status"] = ccLabel.status;
	fields["pid"] = ccLabel.pid;
	fields["agent_id"] = ccLabel.agent_id;
	
	fields["group_label"] = ccLabel.group_label;
	fields["remark_label"] = ccLabel.remark_label;
	fields["create_time"] = ccLabel.create_time;
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLABEL, &fields))
		return false;
	
	ccLabel.label_id = fields["label_id"].GetUInt32();
	return true;
}

// 更新房间数据
bool CenterClubLabelManager::UpdateCenterClubLabel(const CenterClubLabel & ccLabel)
{
	Fields fields;
	fields["status"] = ccLabel.status;
	fields["pid"] = ccLabel.pid;
	fields["agent_id"] = ccLabel.agent_id;
	fields["group_label"] = ccLabel.group_label;
	fields["remark_label"] = ccLabel.remark_label;
	fields["create_time"] = ccLabel.create_time;
	
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLABEL, &fields, "`label_id` = %u", ccLabel.label_id);
	return count > 0;
}

// 获得房间数据
bool CenterClubLabelManager::GetCenterClubLabel(const uint32 & pid, CenterClubLabel * ccLabel)
{
	assert (ccLabel);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBLABEL, &rows, "status < 255 and `pid` = %u", pid);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	ccLabel->label_id = fields["label_id"].GetUInt32();
	ccLabel->status = fields["status"].GetUInt32();
	ccLabel->pid = fields["pid"].GetUInt32();
	ccLabel->agent_id = fields["agent_id"].GetUInt32();
	
	ccLabel->group_label = fields["group_label"].GetValue();
	ccLabel->remark_label = fields["remark_label"].GetValue();
	ccLabel->create_time = fields["create_time"].GetValue();
	return true;
}

//=============================游戏逻辑部分================================
// 创建标签
CenterClubLabel CenterClubLabelManager::CreateCenterClubLabel(CharPtr & pChr)
{
	CenterClubLabel ccLabel;
	if(pChr.isNull())
		return ccLabel;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return ccLabel;
	
	uint32 pid = pUser->getUInt32Field("platform_id");
	uint32 agent_id = pUser->getUInt32Field("reg_from");
	if(GetCenterClubLabel(pid, &ccLabel))
		return ccLabel;
	
	ccLabel.status = 1;
	ccLabel.pid = pid;
	ccLabel.agent_id = agent_id;
	
	ccLabel.group_label = "{\"1\" : \"分组1\", \"2\" : \"分组2\", \"3\" : \"分组3\", \"4\" : \"分组4\", \"5\" : \"分组5\"}";	// 默认分组名称
	// ccLabel.remark_label = "";	// 默认是没有备注标签的
	AddCenterClubLabel(ccLabel);
	
	return ccLabel;
}
	
// 修改分组标签名字
bool CenterClubLabelManager::ChngeClubGroupLabel(CharPtr & pChr, const uint32 & index, String & strData)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClubLabel ccLabel;
	uint32 pid = pUser->getUInt32Field("platform_id");
	if(!GetCenterClubLabel(pid, &ccLabel))
	{// 创建一个新的标签
		ccLabel = CreateCenterClubLabel(pChr);
		if(ccLabel.label_id == 0)
			return false;
	}
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(ccLabel.group_label, val);
	
	uint8 result = enOperatorClubLabelResult_Sucess;
	do
	{
		if(strData.size() > 255)
		{// 标签内容过长
			result = enOperatorClubLabelResult_ContentTooLong;
			break;
		}
		
		if(!val.isMember(StringConverter::toString(index)))
		{// 无效的索引
			result = enOperatorClubLabelResult_ErrorIndex;
			break;
		}
		
	}while(0);
	
	// 发送操作结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateChangeClubGroupLabelPacket(&packet, index, strData, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enOperatorClubLabelResult_Sucess)
	{// 成功修改数据
		val[StringConverter::toString(index)] = strData;
		
		Json::FastWriter w;
		ccLabel.group_label = w.write(val);
		UpdateCenterClubLabel(ccLabel);	
	}
	
	return true;
}

// 检测房间分组是否存在
bool CenterClubLabelManager::IsClubGroupLabelExist(CharPtr & pChr, const uint32 & index, String & strLabel)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClubLabel ccLabel;
	uint32 pid = pUser->getUInt32Field("platform_id");
	if(!GetCenterClubLabel(pid, &ccLabel))
	{// 创建一个新的标签
		ccLabel = CreateCenterClubLabel(pChr);
		if(ccLabel.label_id == 0)
			return false;
	}
	
	Json::Value val;
	Json::Reader reader;
	reader.parse(ccLabel.group_label, val);
	
	if(val.isMember(StringConverter::toString(index)))
	{
		strLabel = val[StringConverter::toString(index)].asString();
		return true;
	}
	
	return false;
}

// 获取玩家备注信息（包括分组和备注列表）
bool CenterClubLabelManager::GetPlayerClubRemarkInfo(CharPtr & pChr)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClubLabel ccLabel;
	uint32 pid = pUser->getUInt32Field("platform_id");
	if(!GetCenterClubLabel(pid, &ccLabel))
	{// 创建一个新的标签
		ccLabel = CreateCenterClubLabel(pChr);
		if(ccLabel.label_id == 0)
			return false;
	}
	
	// 备注玩家列表
	std::list<CenterClubRemark> lstClubRemark;
	sCenterClubRemarkMgr.GetCenterClubRemarkList(&lstClubRemark, pid);
	
	WorldPacket packet;
	if(sProtocolsMgr.CreatePlayerClubRemarkInfoPacket(&packet, ccLabel, lstClubRemark))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	return true;
}
	
//==============================俱乐部标签相关===============================


//==============================俱乐部备注相关===============================

// 添加玩家备注信息
bool CenterClubRemarkManager::AddCenterClubRemark(CenterClubRemark & ccRemark)
{
	Fields fields;
	sDataMgr.initData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBREMARK, &fields);
	
	// uint32 remark_id;
	fields["status"] = ccRemark.status;
	fields["src_pid"] = ccRemark.src_pid;
	fields["src_nick"] = ccRemark.src_nick;
	fields["src_icon"] = ccRemark.src_icon;
	
	fields["dest_pid"] = ccRemark.dest_pid;
	fields["dest_nick"] = ccRemark.dest_nick;
	fields["dest_icon"] = ccRemark.dest_icon;
	
	fields["remark_info"] = ccRemark.remark_info;
	fields["remark_label"] = ccRemark.remark_label;
	fields["group_label"] = ccRemark.group_label;
	fields["create_time"] = ccRemark.create_time;
	
	if (!sDataMgr.insertData(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBREMARK, &fields))
		return false;
	
	ccRemark.remark_id = fields["remark_id"].GetUInt32();
	return true;
}

// 更新玩家备注信息
bool CenterClubRemarkManager::UpdateCenterClubRemark(const CenterClubRemark & ccRemark)
{
	Fields fields;
	fields["status"] = ccRemark.status;
	fields["src_pid"] = ccRemark.src_pid;
	fields["src_nick"] = ccRemark.src_nick;
	fields["src_icon"] = ccRemark.src_icon;
	
	fields["dest_pid"] = ccRemark.dest_pid;
	fields["dest_nick"] = ccRemark.dest_nick;
	fields["dest_icon"] = ccRemark.dest_icon;
	
	fields["remark_info"] = ccRemark.remark_info;
	fields["remark_label"] = ccRemark.remark_label;
	fields["group_label"] = ccRemark.group_label;
	fields["create_time"] = ccRemark.create_time;
	
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBREMARK, &fields, "`remark_id` = %u", ccRemark.remark_id);
	return count > 0;
}

bool CenterClubRemarkManager::UpdateCenterClubRemark(Fields * fields, const char * QueryString, ...)
{
	char sql[SQLBUFER_LEN] = "";
	va_list vlist; 
	va_start(vlist, QueryString); 
	vsnprintf(sql, SQLBUFER_LEN, QueryString, vlist); 
	va_end(vlist);
	int count = sDataMgr.updateDataQueryAffected(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBREMARK, fields, sql);
	return count > 0;
}

// 获得玩家备注信息
bool CenterClubRemarkManager::GetCenterClubRemark(const uint32 & src_pid, const uint32 & dest_pid, CenterClubRemark * ccRemark)
{
	assert (ccRemark);
	FieldRows rows;
	sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBREMARK, &rows, "status < 255 and `src_pid` = %u and `dest_pid` = %u", src_pid, dest_pid);
	if (rows.empty())
		return false;
	
	Fields fields = rows.front();
	ccRemark->remark_id = fields["remark_id"].GetUInt32();
	ccRemark->status = fields["status"].GetUInt32();
	
	ccRemark->src_pid = fields["src_pid"].GetUInt32();
	ccRemark->src_nick = fields["src_nick"].GetValue();
	ccRemark->src_icon = fields["src_icon"].GetValue();
	
	ccRemark->dest_pid = fields["dest_pid"].GetUInt32();
	ccRemark->dest_nick = fields["dest_nick"].GetValue();
	ccRemark->dest_icon = fields["dest_icon"].GetValue();
	
	ccRemark->remark_info = fields["remark_info"].GetValue();
	ccRemark->remark_label = fields["remark_label"].GetValue();
	ccRemark->group_label = fields["group_label"].GetValue();
	ccRemark->create_time = fields["create_time"].GetValue();
	return true;
}

// 获得玩家备注信息列表
bool CenterClubRemarkManager::GetCenterClubRemarkList(std::list<CenterClubRemark> * lstClubRemark, const uint32 & src_pid)
{
	lstClubRemark->clear();
	FieldRows fieldRows;
	
	if (!sDataMgr.getDatas(DataManager::DatabaseTypeCenter, DB_NAME_GAMECLUBREMARK, &fieldRows, "status < 255 and src_pid = %u", src_pid))
		return 0;
	
	FieldRows::iterator iter, enditer = fieldRows.end();
	for (iter = fieldRows.begin(); iter != enditer; ++ iter)
	{
		CenterClubRemark ccRemark;
		Fields &fields = *iter;
		
		ccRemark.remark_id = fields["remark_id"].GetUInt32();
		ccRemark.status = fields["status"].GetUInt32();
		
		ccRemark.src_pid = fields["src_pid"].GetUInt32();
		ccRemark.src_nick = fields["src_nick"].GetValue();
		ccRemark.src_icon = fields["src_icon"].GetValue();
		
		ccRemark.dest_pid = fields["dest_pid"].GetUInt32();
		ccRemark.dest_nick = fields["dest_nick"].GetValue();
		ccRemark.dest_icon = fields["dest_icon"].GetValue();
		
		ccRemark.remark_info = fields["remark_info"].GetValue();
		ccRemark.remark_label = fields["remark_label"].GetValue();
		ccRemark.group_label = fields["group_label"].GetValue();
		ccRemark.create_time = fields["create_time"].GetValue();
		
		lstClubRemark->push_back(ccRemark);
	}
	
	return lstClubRemark->size();
}

//=============================游戏逻辑部分================================
// 设置玩家备注信息（添加/修改）
uint8 CenterClubRemarkManager::SetCenterClubRemarkInfo(CharPtr & pChr, const uint32 & dest_pid, const uint32 & groupLabel_id, String & strLabel, String & strData)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	ResourcePtr pDestUser;
	
	uint32 src_pid = pUser->getUInt32Field("platform_id");
	uint32 gz_id = pChr->getUInt32Field("gz_id");
	String groupLabelName = "";
	uint8 result = enOperatorClubRemarkResult_Sucess;
	do
	{
		if(src_pid == dest_pid)
		{// 不能对自己进行备注
			result = enOperatorClubRemarkResult_CannotOpSelf;
			break;
		}
		
		if(strData.size() > 255)
		{// 备注信息过长
			result = enOperatorClubRemarkResult_RemarkTooLong;
			break;
		}
		
		if(!sCenterClubLabelMgr.IsClubGroupLabelExist(pChr, groupLabel_id, groupLabelName))
		{// 分组不存在
			result = enOperatorClubRemarkResult_ErrorGroup;
			break;
		}
		
		std::list<ResourcePtr> lstUser;
		sUserMgr.getDBObjects(&lstUser, 0, 1, "platform_id = %u and gz_id = %u", dest_pid, gz_id);
		if(lstUser.empty())
		{// 目标玩家不存在
			result = enOperatorClubRemarkResult_NoPid;
			break;
		}
		
		pDestUser = *lstUser.begin();
		if(pDestUser.isNull())
		{// 目标玩家不存在
			result = enOperatorClubRemarkResult_NoPid;
			break;
		}
		
	}while(0);
	
	// 发送修改结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateSetPlayerClubRemarkResultPacket(&packet, dest_pid, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enOperatorClubRemarkResult_Sucess)
	{
		CenterClubRemark ccRemark;
		if(GetCenterClubRemark(src_pid, dest_pid, &ccRemark))
		{// 找到备注信息，修改玩家分区和备注内容
			ccRemark.group_label = groupLabelName;
			ccRemark.remark_info = strData;
			ccRemark.remark_label = strLabel;
			UpdateCenterClubRemark(ccRemark);
		}
		else
		{// 未找到备注信息，添加新的备注信息
			ccRemark.status = 1;
			
			ccRemark.src_pid = src_pid;
			ccRemark.src_nick = pChr->getStringField("name");
			ccRemark.src_icon = pChr->getStringField("desc");
			
			ccRemark.dest_pid = dest_pid;
			ccRemark.dest_nick = pDestUser->getStringField("nick");
			ccRemark.dest_icon = pDestUser->getStringField("banner");
			
			ccRemark.remark_info = strData;
			ccRemark.remark_label = strLabel;
			ccRemark.group_label = groupLabelName;
			
			AddCenterClubRemark(ccRemark);
		}
	}
	
	return 0;
}

// 移除玩家备注信息
uint8 CenterClubRemarkManager::RemoveCenterClubRemarkInfo(CharPtr & pChr, const uint32 & dest_pid)
{
	if(pChr.isNull())
		return false;
	
	UserPtr pUser = sUserMgr.getByHandle(pChr->getUInt32Field("userid"));
	if(pUser.isNull())
		return false;
	
	CenterClubRemark ccRemark;
	uint32 src_pid = pUser->getUInt32Field("platform_id");
	uint8 result = enOperatorClubRemarkResult_Sucess;
	do
	{
		if(!GetCenterClubRemark(src_pid, dest_pid, &ccRemark))
		{// 未找到目标玩家的备注信息
			result = enOperatorClubRemarkResult_NotExistRemarkInfo;
			break;
		}
	}while(0);
	
	// 发送移除结果
	WorldPacket packet;
	if(sProtocolsMgr.CreateRemoveRemarkPlayerPacket(&packet, dest_pid, result))
		sGLMgr.SendProtocolsToChr(pChr, &packet);
	
	if(result == enOperatorClubRemarkResult_Sucess)
	{
		ccRemark.status = 255;
		UpdateCenterClubRemark(ccRemark);
	}

	return 0;
}

//==============================俱乐部备注相关===============================
